user1445685
user1445685

Reputation: 883

Postgresql cross-database references in Rails

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

Upvotes: 0

Views: 121

Answers (1)

ptd
ptd

Reputation: 3053

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.

Upvotes: 1

Related Questions