1
我不知道为什么我找不到什么看起来像是一个非常基本的问题。假设我有类似Rails:在HABTM加入模型中分配额外的列
class Category < ActiveRecord::Base
has_many :categorizations
has_many :items, :through => :categorizations
end
class Item < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
end
class Categorization < ActiveRecord::Base
attr_accessible :some_field
belongs_to :category
belongs_to :item
end
和相关的迁移。那么可以做
item1=Item.new()
item2=Item.new()
foo=Category.new()
foo.items=[ item1, item2 ]
,对吧?那么,如何获得将foo链接到item1和item2的Categorizations,以便设置some_field的值?
您需要将id放入Categorization模型,但任何ActiveRecord对象上的id只有在将其保存到数据库后才会出现。 – 2012-07-25 05:36:58