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?
- 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 notprofile
- 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.
精彩评论