2012-03-08 61 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