问题描述
如何实现 ActiveModel 关联(无表嵌套模型)?
How to impliment ActiveModel associations (tableless nested models)?
例如:
book has many chapters
使用 ActiveRecord,我将创建两个模型并将它们与 has_many 和 belongs_to 相关联.但是 ActiveModel 没有这样的功能.我该如何实施?
With ActiveRecord I would create two models and assosiate them with has_many and belongs_to. But ActiveModel doesn't have such functionality. How can I implement this?
推荐答案
你不能那样做.它不是活动记录.
You simply can't do it that way. It is not active record.
您可以在以下位置查看 ActiveModel 文档(和源代码):
You can check ActiveModel documentation (and source code) at :
https://github.com/rails/rails/tree/master/activemodel
我想你必须用老式的方式来做,使用一系列章节和章节中对本书的引用.
I guess you have to do it old fashion way, using an array of chapters and a reference to the book in the chapters.
希望这会有所帮助!
这篇关于Ruby on Rails 3 (3.1) ActiveModel 关联(无表嵌套模型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!