-1
我很難以深度嵌套的形式訪問fields_for對象的關聯方法。你能幫我理解我哪裏出錯了嗎?如何訪問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
PotentialBuilding型號:
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
...
你能幫我理解嗎?謝謝。