1

運行軌道3.2.1。嘗試什麼應通過相關聯的簡單的嵌套的has_many所示的文檔(http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association):嵌套has_many通過協會不工作

class Company < ActiveRecord::Base 
    has_many :locations, :dependent => :destroy 
    has_many :assets, :through => :locations 
    has_many :components, :through => :assets 
end 

class Location < ActiveRecord::Base 
    belongs_to :company 
    has_many :assets 
    has_many :components, :through => :assets 
end 

class Asset < ActiveRecord::Base 
    belongs_to :location 
    has_many :components 
end 

class Component < ActiveRecord::Base 
    belongs_to :asset 
end 

在控制檯Company.find(2)工作正常以及Company.find(2).locations但不是Company.find(2).assetsCompany.find(2).components。我得到:

1.9.3p0 :071 > Company.find(2).assets 
    Company Load (0.8ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 ORDER BY name ASC LIMIT 1 [["id", 2]] 
NoMethodError: undefined method `assets' for #<Company:0x007f939d714318> 

我似乎在這裏失去了一些東西。我嵌套更深層次,但根據文檔,這應該是好的。

回答

1

哇......讓我瘋狂。希望我能幫助其他人。搜索了控制檯錯誤消息,並且只需要重新載入控制檯...「哦,哦!