开发者

Using linked models in arrary to filter names

Users have one profile, and profiles belong to users.

I'm trying to make a query where searching a user list, and I can search the names which exist in the user's profiles.

Here is what开发者_JAVA百科 I have so far:

@user_list = User.where(:company_id => current_user.company.id)
  .joins(:profile).where("profile.first_name like ?", "%#{params[:q]}%")

This doesn't work, but I'm not really sure where to go from here?


  1. You didn't write the error message or the database table, but i'm guessing that as a rails convention, the table is called profiles and not profile
  2. You should scope by company instead of query by id in the user model

Together:

@user_list = current_user.company.users.
     joins(:profile).where("profiles.first_name like ?", "%#{params[:q]}%")


try User.includes(:profile) instead of joins, and check the resulting sql query in the log. If it does not work, show us the resulting sql.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜