2012-03-08 60 views
0

我有以下型號:NoMethodError?計數記錄

class Label < ActiveRecord::Base 
    has_many :releases 
end 

class Release < ActiveRecord::Base 
    belongs_to :label 
    has_many :products 
    has_and_belongs_to_many :tracks 

    def self.releases_count 
    self.count(:all) 
    end 
end 

class Product < ActiveRecord::Base 
    belongs_to :release 

    has_many :releases_tracks, :through => :release, :source => :tracks  
    has_and_belongs_to_many :tracks 

    def self.products_count 
    self.count(:all) 
    end 
end 

在我的標籤/索引視圖我可以在使用顯示發佈精絕的計數:

<%= label.releases.releases_count %> 

我想用做同樣的產品:

<%= label.releases.products.products_count %> 

卻得到了一個N​​oMethodError:

undefined method `products' for #<Label:0x10ff59690> 

任何想法?

我有很多我想要執行的其他聚合(跟蹤計數等),所以一些指導我要去哪裏錯將非常感激。

回答

3

您需要定義您的生產/標籤協會

class Label < ActiveRecord::Base 
    has_many :releases 
    has_many :products, :through => :releases 
end 
+0

哇,我真的有成效了這一點嘍!在我看來,標籤/發佈和發佈/產品是相關聯的,我認爲我可以通過label.release.product訪問。我做了一個小改動,然後使用<%= label.products.products_count%>進行訪問。謝謝您的幫助!! – Raoot 2012-03-08 10:46:11