0

这是我正在尝试做的事情:

我有三张桌子:

约会用户和共享 - 我想将用户分配给约会。所以到目前为止我所做的是我在约会和用户之间创建了一个表,其中存储了用户分配给的 user_id 和约会 ID。我可以在我的约会控制器中搜索特定约会的股票

@shares = Shares.find(:all, :conditions => { :appointment_id => @appointment.id })

之后,我得到一个哈希 @shares,如下所示:

[#<Shares id: 10, user_id: 3, appointment_id: 1, created_at: "2012-12-04 10:24:16", updated_at: "2012-12-04 10:24:17">, #<Shares id: 12, user_id: 2, appointment_id: 1, created_at: "2012-12-04 10:28:38", updated_at: "2012-12-04 10:28:39">] 

我现在想做的是在 users 表中查询,以查找属于我在共享哈希中找到的 user_ids 的用户名。我尝试过这样的事情:

@shares.each do |s|
@participants = User.all(:conditions => {:id => s.user_id})
end

但它不起作用,因为每次循环开始时参与者哈希都会被覆盖......感谢您的帮助

4

1 回答 1

1
@shares = Shares.find(:all, :conditions => { :appointment_id => @appointment.id })
@participants = User.all(:conditions => ["id IN (?)", @shares.map{|s| s.user_id}.compact])
于 2012-12-04T12:50:29.940 回答