0
중첩 된 속성을 통해 레코드를 저장하면 가상 속성이 하위 모델에서 설정되지 않습니다.accepts_nested_attributes_for를 통해 저장할 때 가상 속성은 nil입니다.
class Person < ActiveRecord::Base
has_many :houses
accepts_nested_attributes_for :houses
end
class House < ActiveRecord::Base
attr_accessor :house_name #virtual
before_save do
puts attributes # doesn't include house_name when saving through parent model
puts @house_name # nil when saving through parent model
end
end
person = Person.find(1)
person.houses.count #=> 3
person.houses.first.house_name = 'crazy house'
person.save # house_name not in attributes
house = person.houses.first
house.house_name = 'moms house'
house.save #house_name is in attributes
그 의미가 있습니다. params 해시를 저장하면 동일할까요? 즉 - @ person.update_attributes (params [: person]) –
'house_name : 'crazy house''는'해시 (Hash)'이므로 대신 자신의 것을 제공 할 수 있습니다. – jdoe
맞지만 그 집은 부모 개체를 통하지 않고 그냥 집을 업데이트하는 것입니다. –