2011-03-07 33 views
0

我有一個充當稱爲CELEBRATIONS的連接表的模型。如何從has_and_belongs_to_many模型關係中訪問表

CELBERATION 
has_and_belongs_to_many :users 
belongs_to :board 

create_table :celebrations do |t| 
     t.column :board_id,  :int, :null => false 
     t.column :user_id,   :int, :null => false 
     t.column :role,   :string, :null => false 
     t.column :token,   :string 
     t.timestamps 
     end 


USER 
has_many :celebrations 

Board 
has_many :celebrations 

慶祝活動表中的角色是:老闆,店長,或朋友

我想了BOARD用戶記錄中,其中的角色是朋友。

我似乎錯過了一些東西。

@invited_friends = User.find(:all, :include => :celebrations, :conditions => ["board_id = ?, role = ?", @board.id, "FRIEND"]) 

任何人都可以點我在正確的指導?先謝謝你。

回答

0

您的模型中存在錯誤的關係。 對於HABTM,

 
CELBERATION 
has_and_belongs_to_many :users 

USER 
has_and_belongs_to_many :celebrations 


And one more table celebrations_users with user_id, celebration_id columns. 
Put the role column in users table. 
+0

謝謝Ashish。我剛剛意識到我認爲我需要重新審視我的關係。 – chell 2011-03-07 07:50:43