我很难以深度嵌套的形式访问 fields_for 对象的关联方法。你能帮我理解我哪里出错了吗?
机会模型:
class Opportunity < ActiveRecord::Base
has_many :seedlings
accepts_nested_attributes_for :seedlings
end
苗木型号:
class Seedling < ActiveRecord::Base
belongs_to :opportunity
belongs_to :potential_building
accepts_nested_attributes_for :potential_building
end
潜力建筑模型:
class PotentialBuilding < ActiveRecord::Base
has_many :seedlings
end
我希望我可以在 hidden_field 旁边写下 potential_building 的名称,但我显然不明白。我可以将 potential_building_id 写入页面,如下所示(haml):
...
= opportunity_form.fields_for :seedlings do |seedling_form|
= seedling_form.hidden_field :potential_building_id
= seedling_form.object.potential_building_id # => 73
....
我希望访问协会的方法会像这样简单,但我不明白为什么不是这样。
...
= opportunity_form.fields_for :seedlings do |seedling_form|
= seedling_form.hidden_field :potential_building_id
= seedling_form.object.potential_building.name # => undefined method `name' for nil:NilClass
...
你能帮我理解吗?谢谢你。