对Activerecord的SQL查询

时间:2014-02-23 14:42:43

标签: sql ruby-on-rails activerecord

有人可以告诉我,我怎么能这样做但是没有SQL,我想用Activerecord做到这一点

     <%= f.association :user, 
         collection: User.where("id not in(select user_id from clientesusers where cliente_id = #{@cliente.id})"),
         label: false, error: false,
         input_html: {class: 'form-control input-sm'} %>

我的意思是`User.where(“id not in(从clientesusers中选择user_id,其中cliente_id = #{@cliente.id})”)没有SQL

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

User.joins("INNER JOIN clients ON clients.user_id <> users.id AND clients.user_id = #{@cliente.id}")