Query records through its belongs_to relation in Rails - ruby-on-rails

I have an Activities model, and they belong_to a Location
How do i select all the activities whose location.country = Australia? (for example)
Can I do this within a scope?

With the latest rails versions you can do:
Activity.joins(:location).where(locations: { country: "Australia" })
Beware:
it is location (singular) in joins(:location) because it references the belongs_to relationship name
it is locations (plural) in where(…) because it references the table name
The latter means that if you had the following:
belongs_to :location, class_name: "PublicLocation"
the query would be:
Activity.joins(:location).where(public_locations: { country: "Australia" })

The kind of query you're talking about is a join. You can try queries like this in the console like:
Activity.joins(:locations).where('locations.country = "Australia"')
This means that SQL is going to take all the activities and locations associated with then, find the locations where country=Australia, and then return you the activities that are associated with those locations.
To make this into a more reusable scope, define it on your model with a variable for country:
scope :in_country, lambda {|country| joins(:locations).where('locations.country = ?',country)}
You can learn more about this in the API docs.

Yes, a scope can be used. Something like this ought to work on the Activities model:
scope :down_under,
joins(:locations).
where("locations.country = 'Australia')

Related

How to get all records that also "own" specific records performantly

I have the following two models in Rails application
class Students < ApplicationRecord
has_many :courses
attr_accessor :name
end
class Courses < ApplicationRecord
has_many :students
attr_accessor :name, :course_id
end
I would like to get a a list of all courses shared for each student who has been in the same class as a selected student in an efficient manner.
Given the following students:
Jerry's courses ["math", "english", "spanish"],
Bob's courses ["math", "english", "french"],
Frank's courses ["math", "basketweaving"]
Harry's courses ["basketweaving"]
If the selected student was Jerry, I would like the following object returned
{ Bob: ["math", "english"], Frank: ["math"] }
I know this will be an expensive operation, but my gut tells me there's a better way than what I'm doing. Here's what I've tried:
# student with id:1 is selected student
courses = Student.find(1).courses
students_with_shared_classes = {}
courses.each do |course|
students_in_course = Course.find(course.id).students
students_in_course.each do |s|
if students_with_shared_classes.key?(s.name)
students_with_shared_classes[s.name].append(course.name)
else
students_with_shared_classes[s.name] = [course.name]
end
end
end
Are there any ActiveRecord or SQL tricks for a situation like this?
I think you're looking to do something like this:
student_id = 1
courses = Student.find(student_id).courses
other_students = Student
.join(:courses)
.eager_load(:courses)
.where(courses: courses)
.not.where(id: student_id)
This would give a collection of other students that took courses with only two db queries, and then you'd need to narrow down to the collection you're trying to create:
course_names = courses.map(&:name)
other_students.each_with_object({}) do |other_student, collection|
course_names = other_student.courses.map(&:name)
collection[other_student.name] = course_names.select { |course_name| course_names.include?(course_name) }
end
The above would build out collection where the key is the student names, and the values are the array of courses that match what student_id took.
Provided you setup a join model (as required unless you use has_and_belongs_to_many) you could query it directly and use an array aggregate:
# given the student 'jerry'
enrollments = Enrollment.joins(:course)
.joins(:student)
.select(
'students.name AS student_name',
'array_agg(courses.name) AS course_names'
)
.where(course_id: jerry.courses)
.where.not(student_id: jerry.id)
.group(:student_id)
array_agg is a Postgres specific function. On MySQL and I belive Oracle you can use JSON_ARRAYAGG to the same end. SQLite only has group_concat which returns a comma separated string.
If you want to get a hash from there you can do:
enrollments.each_with_object({}) do |e, hash|
hash[e.student_name] = e.course_names
end
This option is not as database independent as Gavin Millers excellent answer but does all the work on the database side so that you don't have to iterate through the records in ruby and sort out the courses that they don't have in common.

Rails order results with multiple joins to same table

--Edit--
I wanted to simplify this question.
With this model structure:
has_one :pickup_job, class_name: 'Job', source: :job
has_one :dropoff_job, class_name: 'Job', source: :job
What I want to do is:
Package.joins(:dropoff_job, :pickup_job).order(pickup_job: {name: :desc})
This is obviously not valid, the actual syntax that should be used is:
.order('jobs.name desc')
However the way that rails joins the tables means that I cannot ensure what the table alias name will be (if anything), and this will order by dropoff_job.name instead of pickup_job.name
irb(main):005:0> Package.joins(:dropoff_job, :pickup_job).order('jobs.name desc').to_sql
=> "SELECT \"packages\".* FROM \"packages\" INNER JOIN \"jobs\" ON \"jobs\".\"id\" = \"packages\".\"dropoff_job_id\" INNER JOIN \"jobs\" \"pickup_jobs_packages\" ON \"pickup_jobs_packages\".\"id\" = \"packages\".\"pickup_job_id\" ORDER BY jobs.name desc"
Also I am not in control of how the tables are joined, so I cannot define the table alias.
--UPDATE--
I have had a play with trying to extract the alias names from the current scope using something like this:
current_scope.join_sources.select { |j| j.left.table_name == 'locations' }
But am still a little stuck and really feel like there should be a MUCH simpler solution.
--UPDATE--
pan's answer works in some scenarios but I am looking for a solution that is a bit more dynamic.
Use the concatenation of association name in plural and current table name as a table alias name in the order method:
Package.joins(:dropoff_job, :pickup_job).order('pickup_jobs_packages.name desc')
You can try something like this
Package.joins("INNER JOIN locations as dropoff_location ON dropoff_location.id = packages.dropoff_location_id INNER JOIN locations as pickup_location ON pickup_location.id = packages.pickup_location_id)
Idea is to create your own alias while joining the table

Pluck associated model's attribute in Rails query

In my rails app, collections have many projects, and projects have many steps.
I'd like to grab all the ids of steps in a collection's projects, and I'm wondering if I can do it all in one query.
For example, I know I can do the following
step_ids = []
#collection.projects.each do |project|
project.steps.each do |step|
step_ids << step.id
end
end
But is it possible to do something like the following:
#collection.projects.include(:steps).pluck("step.id") // syntax here is not correct
Try this:
Step.joins(:project).where(projects: { collection_id: #collection.id }).pluck(:'steps.id')
Note the use of project for the joins, and then projects for the where clause. The first corresponds to the belongs_to relationship, and the latter is the name of the db table.
edit: in the case of a many-to-many relationship between projects and collections, and assuming a project belongs_to a project_collection (and then has_many :collections, through :project_collection)
Step.joins(:project => :project_collection)
.where(project_collections: { collection_id: #collection.id })
.pluck(:'steps.id')
Unfortunately, I don't think that we could do it through AR in a single query. You could do a nested query below to retrieve it in two queries to the database:
Step.includes(:projects)
.where(projects: { id: Projects.includes(:collections)
.where(collections: { id: #collections.id }).pluck(:id) } )
.pluck(:id)

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
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.

ActiveRelation where statement on child attribute

I have a has_one condition that I'm trying to access but am having a little trouble
Solicitation belongs_to :lead
Lead has_many :solicitations
My first statement grabs all solicitations for a user
#solicitations = current_user.solicitations.includes(:lead)
I can already access the attribute lead.case_type and could just cycle through the relation and put them in their places manually, but I figure their is an easier way.
What I am trying to do is something similar to
#solicitations.where("lead.case_type = ?", "Civil")
I have tried these and receive an unknown column error lead.case_type
Solicitation.all(:conditions => {:lead => {:case_type => 'Civil'}}, :joins => :lead)
The problem is that you are using lead.case_type, but (if you're following Rails' conventions) your table name is leads. This should work:
#solicitations = current_user.solicitations.includes(:lead).where("leads.case_type = ?", "Civil")
You could also use joins for that:
#solicitations = current_user.solicitations.joins(:lead).where("leads.case_type = ?", "Civil")
includes does an outer join, whereas joins does an inner join. Since you're querying the joined table an inner join would be better here.
In where you always have to use the table name (plural), but in includes and joins it depends on the relationship. In this case solicitation belongs to lead, so you have to use :lead (singular). It's a bit confusing, but I hope this clears it up for you.

Resources