Rails 3: Trying to understand join query
I have a User class and a GroupUser class. I开发者_Python百科'm trying to do a search by name of the users. I tried following what I read on the joins, but I have something wrong. Also I need to change my name portion of the query to a like instead of an equals
Here is the query I had initially built.
@users = GroupUser.joins(:users).where(:group_id => params[:group_id]).where(:users => {:name => params[:q]})
Try this:
@users = User.where("name ilike ? and id in (select distinct user_id from groups_users where group_id = ?)", "%#{params[:q]}%", params[:group_id])
精彩评论