开发者

Rails 3: Trying to understand join query

开发者 https://www.devze.com 2023-03-25 06:26 出处:网络
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

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])
0

精彩评论

暂无评论...
验证码 换一张
取 消