Get parent objects without active childs - sql

I've two models, Doctor and DoctorClinic where a doctor has_many clinics.
doctor.rb:
has_many :clinics, class_name: 'DoctorClinic', dependent: :destroy
doctor_clinic.rb
belongs_to :doctor
DoctorClinic have doctor_id and a boolean active field.
What I want:
I want to get all doctors which does not have any active (active field is true) clinics. If a doctor have two clinics, out of which one is active and other is inactive then doctor should not be selected.
Doctor record will be selected if,
there's no clinics at all
if there's any clinic but all are inactive, i.e. all have active false.
Doctor will not be selected if,
there's any active clinics.
What I've tried so far:
Try 1:
scope :incomplete_doctors, -> { includes(:clinics)
.where("( doctor_clinics.id IS NULL ) OR
( doctor_clinics.id IS NOT NULL AND
doctor_clinics.active=?)", false )
}
Try 2:
scope :incomplete_doctors, -> { where("id NOT IN (?)", self.includes(:clinics)
.where("( doctor_clinics.doctor_id IS NULL ) OR
( doctor_clinics.doctor_id IS NOT NULL AND
doctor_clinics.active=?)", false )
.select(:id))
}
Try 3:
SELECT "doctors".* FROM "doctors"
LEFT OUTER JOIN "doctor_clinics" ON "doctor_clinics"."doctor_id" = "doctors"."id"
WHERE ( ( doctor_clinics.id IS NULL ) OR
( doctor_clinics.id IS NOT NULL AND
doctor_clinics.active='f'))
GROUP BY doctors.id
HAVING 'true' <> ANY(array_agg(DISTINCT doctor_clinics.active::TEXT));
Success:
I'm able to achieve desired output using following method, but I want to achieve this using a SQL query.
def active_clinics
clinics.active_clinics # active_clinics is a scope in Clinic model while give all active clinics
end
def self.incomplete_doctors
(Doctor.all.map { |d| d unless d.active_clinics.present? }).compact
end

Something like this should do the trick in pure SQL
SELECT *
FROM doctors
WHERE NOT EXISTS (
SELECT 1
FROM doctor_clinics
WHERE
doctor_clinics.doctor_id = doctors.id
AND doctor_clinics.active = true
)
You should be able to use it with find_by_sql:
Doctor.find_by_sql(SQL)
Have no Rails 3 project to actually test it;-)

Related

Group entries based on associated model fields in aggregate

I have two models, Keyword and Company, associated using has_and_belongs_to_many.
On the Keyword model, there is a boolean field included. If this is set to true, any Company associated with that Keyword should be considered included, unless it has any associated Keywords with included set to false. included can also be set to nil which I consider a state of "empty".
To summarize:
Company A has 2 associated Keywords, 1 included = true and 1 included = nil. This company is INCLUDED.
Company B has 2 associated Keywords, 1 included = false and 1 included = true. This company is EXCLUDED.
Company C has 2 associated Keywords, both included = nil. This company is EMPTY.
What is the best way to 1) count the number of Included, Excluded, and Empty companies, and 2) query/scope the Company model to Included, Excluded, or Empty?
The current solution I have hacked together is causing expensive queries that are resulting (usually) in request timeouts. Models follow:
class Company < ApplicationRecord
has_and_belongs_to_many :keywords
scope :included, -> { joins(:keywords).merge(Keyword.included).group('id').reorder('') }
scope :excluded, -> { joins(:keywords).merge(Keyword.excluded).group('id').reorder('') }
scope :empty, -> { joins(:keywords).merge(Keyword.empty).group('id').reorder('') }
end
class Keyword < ApplicationRecord
has_and_belongs_to_many :companies
scope :excluded, -> { where(included: false) }
scope :included, -> { where(included: true) }
scope :empty, -> { where(included: nil) }
end
(reorder is included in Company model to resolve a quirk with pg_search gem and grouping)
I´m always trying to avoid joins because they are creating duplicates that you´d have to remove with a group('id').
I´d rather use EXISTS.
class Company < ApplicationRecord
has_and_belongs_to_many :keywords
scope :included, -> do
where(
Keyword
.included
.joins("INNER JOIN companies_keywords ON companies_keywords.keyword_id = keywords.id AND company_id = companies.id")
.arel.exists
).where.not(
Keyword
.excluded
.joins("INNER JOIN companies_keywords ON companies_keywords.keyword_id = keywords.id AND company_id = companies.id")
.arel.exists
)
end
scope :excluded, -> do
where(
Keyword
.excluded
.joins("INNER JOIN companies_keywords ON companies_keywords.keyword_id = keywords.id AND company_id = companies.id")
.arel.exists
)
end
scope :empty, -> do
where.not(
Keyword
.joins("INNER JOIN companies_keywords ON companies_keywords.keyword_id = keywords.id AND company_id = companies.id")
.where(included: [true, false])
.arel.exists
)
end
end
Also i recently discovered this Gem which simplifies exists queries. I find that particularly helpful.
You could just write:
scope :included, -> { where_exists(:keywords, &:included).where_not_exists(:keywords, &:excluded) }
I didn´t test the performance on these but it should be pretty solid (as long as you added the right indexes).
Hope this answers your questions.

Compare the last element in Rails active record

I have two models Student and StudentRecord. Student Record has start_date, end_date and class_course_id among its attributes and it belongs to Student
scope = Student.eager_load(:student_record)
Now, I want to get the students whose latest(according to start_date) student_record's class_course_id is the same as a given class_course_id. something like:
scope = scope.where("student_records.order(start_date asc).last.class_course_id = ?", params[:class_course_id])
Obviously, the above statement is not correct but I hope it describes what I would like to get.
The below should do
Student.eager_load(:student_record).where("student_records.class_course_id = ?", params[:class_course_id]).order('student_records.start_date asc').last
Use Order by clause and descend to get the latest dates .order("student_records.start_date DESC") and in the where clause, the records will be filtered out .where("student_records.class_course_id = ?", params[:class_course_id]). The where will come first then, the order by desc will sort it correctly.
scope.where("student_records.class_course_id = ?", params[:class_course_id]).order("student_records.start_date DESC")
And you can do .limit(5) to get the first 5 records that are the latest start_dates.
If you want all students, then this is a bit non-trivial in active record.
Identifying the last Student Record sounds like an important thing that would benefit from a scope:
def self.latest_for_student
where("not exists (select null from student_records sr2 where sr2.student_id = student_records.student_id and sr2.start_date > student_records.start_date)")
end
... which means "return rows for which there does not exist another row in student_records for the same student_id, and a greater start_date"
Or...
def self.latest_for_student
where("student_records.start_date = (select max(start_date) from student_records sr2 where sr2.student_id = student_records.student_id)")
end
... which means "return rows for which the start date is equal to the maximum start date for that student id"
Then you can:
class Student
has_one :last_student_record, -> {merge(StudentRecord.latest_for_student)}, :class_name => "StudentRecord"
has_one :last_class_course, :through => :last_student_record, :source => :class_course
end
class ClassCourse
has_many :last_records_for_student, -> {merge(StudentRecord.latest_for_student)}, :class_name => "StudentRecord"
has_many :students_having_as_last_course, :through => : last_records_for_student, :source => :student
end
Then you should be able to:
#course.students_having_as_last_course
Bit complex ... could be syntax errors ... let me know if there are.

Query using condition within an array

I have 2 models, user and centre, which have a many to many relationship.
class User < ActiveRecord::Base
attr_accessible :name
has_and_belongs_to_many :centres
end
class Centre < ActiveRecord::Base
attr_accessible :name, :centre_id, :city_id, :state_id
has_and_belongs_to_many :users
end
Now I have an user with multiple centres, and I want to retrieve all the centres that have the same "state_id" as that user.
This is what I am doing now
state_id_array = []
user.centres.each do |centre|
state_id_array << centre.state_id
end
return Centre.where("state_id IN (?)", state_id_array).uniq
It works, but it's very ugly. Is there a better way for achieving this? Ideally a one line query.
UPDATE
Now I have
Centre.where('centres.state_id IN (?)', Centre.select('state_id').joins(:user).where('users.id=(?)', user))
The subquery work by itself, but when I tried to execute the entire query, I get NULL for the inner query.
Centre.select('state_id').joins(:user).where('users.id=(?)', user)
will generate
SELECT state_id FROM "centres" INNER JOIN "centres_users" ON "centres_users"."centre_id" = "centres"."id" INNER JOIN "users" ON "users"."id" = "centres_users"."user_id" WHERE (users.id = (5))
Which return 'SA', 'VIC', 'VIC'
but the whole query will generate
SELECT DISTINCT "centres".* FROM "centres" WHERE (centres.state_id IN (NULL,NULL,NULL))
Does user also has state_id column if yes then try this,
User.joins("LEFT OUTER JOIN users ON users.state_id = centers.state_id")
else
try User.joins(:center)
Solved.
.select(:state_id)
will retrieve a model with only the state_id column populated. To retrieve a field, use
.pluck(:state_id)
Below is the final query I had
Centre.where('centres.state_id IN (?)', Centre.joins(:user).where('users.id=(?)', user).pluck('state_id').uniq)

How to select only unique entries from a join table

I would like to make my block of code more efficient. I have two models and a join table for them. They both have a has_many :through relationship. Some parts belong to multiple groups, some only belong to one. I need to get the records that belong to only one group and in the most efficient manner as there are thousands of parts. Here are my models:
part.rb
class Part < ActiveRecord::Base
attr_accessible :name,
:group_ids
has_many :part_groups, dependent: :destroy
has_many :groups, through: :part_groups, select: 'groups.*, part_groups.*'
end
group.rb
class Group < ActiveRecord::Base
attr_accessible :name,
:part_ids
has_many :part_groups, dependent: :destroy
has_many :parts, through: :part_groups, select: 'parts.*, part_groups.*'
end
part_group.rb
class PartGroup < ActiveRecord::Base
attr_accessible :part_id,
:group_id
belongs_to :part
belongs_to :group
end
What I would like to be able to do is get all the parts that belong only to Group A and only to Group B, but not ones that belong to both A & B. After struggling with this for hours and getting nowhere I'm using this as a stop gap:
#groupA = []
#groupB = []
Part.all.each do |part|
if part.group_ids.length == 1
if part.group_ids.first == 1
#groupA.push(part)
elsif part.group_ids.first == 2
#groupB.push(part)
end
end
end
This obviously isn't scalable as there will be many groups. I've tried various methods of join and include that I've been googling but so far nothing has worked.
I am also new to rails , So as far i understand this is the structure of your tables.
parts
Id | Name
groups
Id | Name
part_groups
Id | part_id | group_id
So you can do the following,
Group.find(1).parts // Parts belong to group A
Group.find(2).parts // Parts belong to group B
so this may give parts that belong to other groups also.
Objective is to get parts that belongs only to group A and only to group B
Try for
Group.find(1).parts.collect{|row| row if row.groups.count==1}.flatten
I think this is better approach than yours , because am traversing only those parts which belong to group1.
The raw sql for this could look like
select parts.* from parts
inner join part_groups on parts.id = part_groups.part_id
left outer join part_groups as group_b on group_b.part_id = parts.id and group_b.group_id = 456
where group_b.id is null and part_groups.group_id = 123
Assuming that group a had id 123 and group b had id 456.
What this does is try to join the part_groups table twice (so an alias needs to be used the second time), once where group_id matches group A and once against group B. The use of the left join allows us to require that the second join (against B) produces no rows.
Activerecord doesn't provide much assistance for this, other than allowing you to pass arbitrary sql fragments to joins, so you end up with something like
Part.select("parts.*").
.joins(:part_groups).
.joins("left outer join part_groups as group_b on group_b.group_id = #{groupb.id} and group_b.part_id = parts.id").
.where(:part_groups => {:group_id => groupa.id}).where("group_b.id is null")
Arel (which underpins the query generation part of active record) can generate this sort of query but this isn't exposed directly.

Custom select on join table

I'm using rails 3.2, and trying to use ActiveRecord to query my database.
I have 2 activerecord models, Admin and Order:
class Admin < ActiveRecord::Base
attr_accessible :email, :name
has_many :orders
class Order < ActiveRecord::Base
attr_accessible :operation
belongs_to :admin
In my case, order.operation is a string, representing order type. I'm trying to build a query giving me three columns:
admin.name, admin.orders.where(:operation => 'bonus').count, admin.orders.where(:operation => 'gift').count
Is there a way to fit it in a single query?
EDITED:
here's raw sql to get what I need:
SELECT t_a.admin_id_com, t_a.name, gb_f_f_gb_f_fi.bonus_count, gb_f_f_gb_f_fi.gifts_count
FROM (
SELECT f_fil.admin_id_gifts, f_fil.gifts_count, f_filt.admin_id_bonus, f_filt.bonus_count
FROM (
SELECT admin_id as admin_id_gifts, count(distinct id) as gifts_count FROM orders WHERE operation = 'new gifts!' GROUP BY admin_id_gifts)
f_fil LEFT OUTER JOIN (
SELECT admin_id as admin_id_bonus, count(distinct id) as bonus_count FROM orders WHERE operation = 'new bonuses!' GROUP BY admin_id_bonus)
f_filt ON (f_fil.admin_id_gifts = f_filt.admin_id_bonus))
gb_f_f_gb_f_fi LEFT OUTER JOIN (
SELECT id AS admin_id_com, t_ad.name FROM admins t_ad) t_a ON (gb_f_f_gb_f_fi.admin_id_gifts = t_a.admin_id_com)
Is it possible to buid a query like that using ActiveRecord?
Try this:
#admins = Admin.joins(:orders).
select("admins.id, admins.name, orders.id,
SUM((orders.operation = 'bonus')::integer) AS bonus_count,
SUM((orders.operation = 'gift')::integer) AS gift_count ").
group("admins.id ")
# access each columns as
admin.name, admin.bonus_count, admin.gift_count
Other option is to use eager loading, it will use two queries but might be faster
#admins = Admin.includes(:orders)
# in admin.rb
def orders_count(type)
# Don't use where here as it would make a separate query instead of using eager loading records
orders.select{|x| x.operation == type}.count
end
# access each columns as
admin.name, admin.orders_count("bonus"), admin.orders_count("gift")