2011-03-07 28 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