2010-10-01 27 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