2010-10-01 54 views
1

事件我如何计算用户衬衫尺寸?

has_many :squads, :dependent => :destroy 
has_many :users, :through => :squads, :source => :squad_users 

has_many :squad_users, :dependent => :destroy 
has_many :users, :through => :squad_users 

用户

has_many :squad_users 
has_many :squads, :through => :squad_users 
has_many :events, :through => :squads 

SquadUser

belongs_to :squad 
belongs_to :user 

我创建在用户模式中的一些named_scopes如下:

named_scope :xtralarge, :conditions => [ "shirt = ?", "XL"] 
named_scope :large, :conditions => [ "shirt = ?", "L"] 
named_scope :medium, :conditions => [ "shirt = ?", "M"] 
named_scope :small, :conditions => [ "shirt = ?", "S"] 
named_scope :xtrasmall, :conditions => [ "shirt = ?", "XS"] 

尝试这样:

<%= @event.users.large.size %> 

,我也得到:

undefined method `large' for SquadUser(squad_id: integer, user_id: integer):Class 

我的理解......没有对SquadUser模型中没有 “大” 的属性。

我不明白的是如何在我想要得到什么?一个简单的方法来计算我需要订购的每个事件每个尺寸的衬衫数量: -/

回答

0

您的建模协会是根本上不正确。你试图通过has_many来做一个has_many。 Rails默认不支持。你可以安装这个插件,然后也许你的关联会起作用。一个严肃的建议是简化你的数据模型。在您对关联进行更改或安装插件后,任何一种情况都会发布您的结果。

script/plugin install git://github.com/JackDanger/nested_has_many_through.git