I have a Rails scope query that is returning duplicates. Can anyone see what's the problem with it?
Here's the code:
scope :visible_to_user, lambda { |user|
{
:joins => 'LEFT JOIN SCHEMA.groups ON groups.id = uploaded_files.group_id
LEFT JOIN uploaded_file_references ON uploaded_files.id = uploaded_file_references.uploaded_file_id
LEFT JOIN message_threads ON message_threads.id = uploaded_file_references.thread_id
LEFT JOIN thread_participants ON thread_participants.message_thread_id = message_threads.id',
:conditions => [
%{
uploaded_files.in_private_conversation = false
AND ( ( NOT COALESCE(groups.private, false) )
OR uploaded_files.group_id IN (?)
OR ( thread_participants.referenced_id = '?'
AND thread_participants.referenced_type = 'User')
)
}, user.group_ids, user.id
]
}
}
In rails >= 3:
You can add .uniq call to any scope to not return duplicates, like so:
MyTable.a_scope.where(something).uniq
In rails < 3:
you have to add it by hand with a select configuration like so:
MyTable.find(:all, :select => "distinct my_table.*")
Related
I have a model Category that has_many Pendencies. I would like to create a scope that order the categories by the amount of Pendencies that has active = true without excluding active = false.
What I have so far is:
scope :order_by_pendencies, -> { left_joins(:pendencies).group(:id).order('COUNT(pendencies.id) DESC')}
This will order it by number of pendencies, but I want to order by pendencies that has active = true.
Another try was:
scope :order_by_pendencies, -> { left_joins(:pendencies).group(:id).where('pendencies.active = ?', true).order('COUNT(pendencies.id) DESC')}
This will order by number of pendencies that has pendencies.active = true, but will exclude the pendencies.active = false.
Thank you for your help.
I guess you want to sort by the amount of active pendencies without ignoring categories that have no active pendencies.
That would be something like:
scope :order_by_pendencies, -> {
active_count_q = Pendency.
group(:category_id).
where(active: true).
select(:category_id, "COUNT(*) AS count")
joins("LEFT JOIN (#{active_count_q.to_sql}) AS ac ON ac.category_id = id").
order("ac.count DESC")
}
The equivalent SQL query:
SELECT *, ac.count
FROM categories
LEFT JOIN (
SELECT category_id, COUNT(*) AS count
FROM pendencies
GROUP BY category_id
WHERE active = true
) AS ac ON ac.category_id = id
ORDER BY ac.count DESC
Note that if there are no active pendencies for a category, the count will be null and will be added to the end of the list.
A similar subquery could be added to sort additionally by the total amount of pendencies...
C# answer as requested:
method() {
....OrderBy((category) => category.Count(pendencies.Where((pendency) => pendency.Active))
}
Or in straight SQL:
SELECT category.id, ..., ActivePendnecies
FROM (SELECT category.id, ..., count(pendency) ActivePendnecies
FROM category
LEFT JOIN pendency ON category.id = pendency.id AND pendnecy.Active = 1
GROUP BY category.id, ...) P
ORDER BY ActivePendnecies;
We have to output ActivePendnecies in SQL even if the code will throw it out because otherwise the optimizer is within its rights to throw out the ORDER BY.
For now I developed the following (it's working, but I believe that it's not the best way):
scope :order_by_pendencies, -> { scoped = Category.left_joins(:pendencies)
.group(:id)
.order('COUNT(pendencies.id) DESC')
.where('pendencies.active = ?', true)
all = Category.all
(scoped + all).uniq}
I'm using a find_by_sql method to search users in my userstable.
is there a possibility to use rails code in the select statement?
User.find_by_sql ["SELECT DISTINCT
users.*
FROM
users
JOIN
clients_courses cc
ON
cc.client_id = users.client_id
LEFT JOIN
memberships m
ON
m.user_id = users.id AND m.course_id = cc.course_id
WHERE
cc.course_id = ?
AND
m.user_id IS NULL
AND
users.active = ?
AND
users.firstname LIKE ? or users.lastname LIKE ?
AND NOT IN ( RAILS CODE )", self.id, true, "#{search}%", "#{search}%"]
end
I Marked the position with RAILS CODE
I want to do someting linke this:
Membership.where("course_id = ?", self.id).users
is there a way to do this?
You can do this -
member_user_ids = []
Membership.where("course_id = ?", self.id).map{|membership| membership.users.map{|user| member_user_ids << user.id}}
# you might want to put a uniq! on member_user_ids
User.find_by_sql ["SELECT DISTINCT
users.*
FROM
users
JOIN
clients_courses cc
ON
cc.client_id = users.client_id
LEFT JOIN
memberships m
ON
m.user_id = users.id AND m.course_id = cc.course_id
WHERE
cc.course_id = ?
AND
m.user_id IS NULL
AND
users.active = ?
AND
users.firstname LIKE ? or users.lastname LIKE ?
AND users.id NOT IN ( #{member_user_ids.join(',')} )", self.id, true, "#{search}%", "#{search}%"]
You can also have a look at link which explains how to put array of strings in where clause.
I have the following query in my rails app but its result does not have attributes name so I am not able to use it in amcharts
EmployeeDepartment.joins(:states).group
("employee_departments.name").count
the result is : {"Academic Support":1}
how to make it like this {"department_name":"Academic Support","department_count":1}
Let's say you have a hash like this:
hash = { "Academic Support" => 1, "Another Department" => 3, "Something Else" => 4 }
You can just use map to transform it into an array of hashes containing what you need.
hash.map { |k, v| { "department_name" => k, "department_count" => v } }
=> [{"department_name"=>"Academic Support", "department_count"=>1},
{"department_name"=>"Another Department", "department_count"=>3},
{"department_name"=>"Something Else", "department_count"=>4}]
If your hash only ever contains one key/value pair and you just want another hash, you could try this:
Hash[[["department_name", "department_count"], hash.first].transpose]
Or even simpler...
{ "department_name" => hash.keys.first, "department_count" => hash.values.first }
I solved my problem by this steps:
the following code:
EmployeeDepartment.joins(:states).group
("employee_departments.name").count
generates this in rails console
SELECT COUNT(*) AS count_all, employee_departments.name AS employee_departments_name FROM "employee_departments" INNER JOIN "tickets" ON "tickets"."employee_department_id" = "employee_departments"."id" INNER JOIN "states" ON "states"."id" = "tickets"."state_id" GROUP BY employee_departments.name
i used what was generated in the console in the following:
#department_count = EmployeeDepartment.find(:all,
:select => 'employee_departments.name AS employee_departments_name, COUNT(*) AS department_counter',
:joins => 'INNER JOIN tickets ON tickets.employee_department_id = employee_departments.id INNER JOIN states ON states.id = tickets.state_id',
:group => 'employee_departments.name')
Now the result in amcharts is:
var chartData = [{"department_counter":1,"employee_departments_name":"Academic Support"}];
The line like this (split into multiple lines for readability):
ValueMapping
.joins(:variation)
.where(
:mapper_id => 1,
:variations => {:name => 'de'}
)
.update_all(:value => 'Beispiel')
produces following SQL statement:
UPDATE value_mappings
SET value = 'Beispiel'
WHERE value_mappings.id IN (
SELECT value_mappings.id
FROM
value_mappings
INNER JOIN variations ON variations.id = value_mappings.variation_id
WHERE
value_mappings.mapper_id = 1
AND variations.name = 'de'
)
But I would like to get more PostgreSQL-specific request:
UPDATE value_mappings
SET value = 'Beispiel'
FROM
variations
WHERE
variations.id = value_mappings.variation_id
AND value_mappings.mapper_id = 1
AND variations.name = 'de'
)
Is it possible with ActiveRecord?
How would you write this line in the "rails way"?
unique_attendees = CourseSessionsUser.count_by_sql(["SELECT count(DISTINCT csu.user_id) AS count_user_id FROM course_sessions_users csu, course_sessions cs WHERE cs.course_id = ? AND csu.course_session_id = cs.id"], #course.id)
The query itself is:
SELECT count(DISTINCT csu.user_id) AS count_user_id
FROM course_sessions_users csu,
course_sessions cs
WHERE cs.course_id = ?
AND csu.course_session_id = cs.id
Use count method of the rails
count = CourseSessionsUser.count('csu.user_id',
:distinct => true, :conditions => ["cs.course_id = ?", #course.id]
:joins => "cs LEFT JOIN course_sessions_users csu ON cs.id = csu.course_session_id")
This will return directly non zero integer if condition matches otherwise return zero