【发布时间】:2021-01-28 12:23:12
【问题描述】:
我有一个关于 Rails 嵌套属性的问题。 我正在使用 Rails 4 并且有这个模型:
model Location
has_one parking_photo
has_many cod_photos
accepts_nested_attributes_for :parking_photo
accepts_nested_attributes_for :cod_photos
end
当我使用例如:
Location.find(100).update(cod_photo_ids: [1,2,3]) 有效。
但是Location.find(100).update(parking_photo_id: 1) 不起作用。
我不知道嵌套属性 has_one 和 has_many 有什么区别。
或者当我已经有子对象并且想要将父对象链接到子对象并且不想使用子更新时,我们是否有任何解决方案。
谢谢。
【问题讨论】:
-
你可以试试
Location.find(100).update( parking_photo: ParkingPhoto.find(1) ),它应该改变子对象中的location_id。
标签: ruby-on-rails nested-attributes accepts-nested-attributes