我有那个Parent模型has_one Child。但是,出于某种逻辑原因,我需要将外键存储在Parent而不是Child.
是否可以在表中定义has_one与外键的关系Parent?
我不想定义Parent belongs_to Child,因为我想Parent通过定义来创建这些对象accepts_nested_attributes_for。
我有那个Parent模型has_one Child。但是,出于某种逻辑原因,我需要将外键存储在Parent而不是Child.
是否可以在表中定义has_one与外键的关系Parent?
我不想定义Parent belongs_to Child,因为我想Parent通过定义来创建这些对象accepts_nested_attributes_for。
那是has_one-的倒数belongs_to。
有关详细信息,请参阅http://guides.rubyonrails.org/association_basics.html#choosing-between-belongs-to-and-has-one。
如果你想要一些类似的东西,accepts_nested_attributes_for你可以写你自己的吸气剂吗?
def child_attributes=(attrs)
child_id? ? (child = Child.create(attrs)) : child.update_attributes(attrs))
end
child = Child.create(attrs)save你不是Parent吗