0

I'm building a search for and got stuck on a cross-database references error

activities = Activity.order(:name).includes([:profile => :country])
activities = activities.where("lower(activities.city) like ?", "%#{params[:activity_search][:city].downcase}%") unless params[:activity_search][:city] == ""      
activities = activities.where("activities.sport_id =?", params[:activity_search][:sport_id])

I'm trying to add something like this:

activities = activities.where("activities.profiles.country.id =?", params[:activity_search][:country_id])

an activity's country is the same as the activity creator's country.

How can I add this constraint in my query?

Thanks for your help

1 Answer 1

1

You need to use joins here to include the associations:

activities.joins(profiles: :country).where('countries.id = ?', params[:whatever])

This assumes Activity has_many :profiles and Profile has_one :country and countries is your table name, but I think all that is true based on your post. This will include the associated profiles and country with activities and allow you to use their attributes in the where method call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.