【发布时间】:2011-10-22 04:44:09
【问题描述】:
我得到一个 metawhere 连接依赖错误,内容如下:
@companies_user = Company.joins(:contact).where(:contact => {:user_id => current_user.id}).uniq
公司有_许多联系人
换句话说,每个联系人都有一个方法 company_id
我想显示那些联系人的 user_id 与进行搜索的用户相同的公司(只有一个)。
【问题讨论】:
我得到一个 metawhere 连接依赖错误,内容如下:
@companies_user = Company.joins(:contact).where(:contact => {:user_id => current_user.id}).uniq
公司有_许多联系人
换句话说,每个联系人都有一个方法 company_id
我想显示那些联系人的 user_id 与进行搜索的用户相同的公司(只有一个)。
【问题讨论】:
好吧,如果您的公司有很多联系人,那么您应该包括 contacts 而不是 contact
@companies_user = Company.joins(:contacts).where(:contacts => {:user_id => current_user.id}).uniq
【讨论】:
为什么不这样做呢?
current_user.contact.companies
然后你可以使用 .limit 只获取一个...
【讨论】: