Move specific item to last position - ruby-on-rails

Let there is a model Item with attributes id, name and specific record in DB with name 'other'.
How to get in single SQL query an ActiveRecord::Relation object with items sorted in way that other item is at last position?
As temporarily solution I used Item.where.not(name: 'other') + Item.where(name: 'other') but it results in 2 queries.
NB: it's not a real code but an extremely simplified example.

Perhaps something like below will help - we read all Items, and use slice to remove the Item with name equal to other, and append it back to array at the end.
a = Item.all.tap do |arr|
arr << arr.slice!(arr.index { |t| t.name == "other"})
end

if i get you right then you're looking for a SQL query, right? Then you could try something like that:
select id, name
from (
select id, name, decode(name, 'other', 999, 1) as sort
from items
)
order by sort
I have no database right now to test the statement. But I think you get the idea.
Good luck

Resolved with adding class method to Item model:
def self.sorted
find_by_sql("SELECT * FROM items ORDER BY (CASE name WHEN 'other' THEN 0 ELSE 1 END) DESC, id ASC")
end

Related

Rails distinct values of a value returned by a method in model

Suppose I have an Employee model, there is a method in Employee model named def fixed
def fixed
return self.cached_fixed.to_f if self.cached_fixed.present?
return (self.current_salary && self.current_salary.fixed).to_f
end
end
def current_salary
return #current_salary if #current_salary
# #current_salary = self.employee_salaries.detect{|es| es.end_date == nil}
#current_salary = self.db_current_salary
return #current_salary
end
if the fixed were a column in employee table we could have just used Employee.distinct.select(:fixed) to pull the distinct values
is there a way if it's just a method not a field in table without loading all the employees.
I am expecting to get the unique values of a column from a table , but it may not be a column as in the above table
Not for an arbitrary method, no. But you start the query from the EmployeeSalary end and fetch only the column you care about in one query using select:
EmployeeSalary
.select(:fixed)
.join(:employee)
.where(end_date: nil)
This will run a select fixed from... query and return a list of EmployeeSalary objects, but all the fields that aren't listed in the select call will be nil. Assuming the constraint of only one salary record having end_date: nil, there will be one EmployeeSalary object per employee. You can add .distinct in the method chain if you want unique values.
I'm not sure how the caching logic fits into this question. You can apply caching logic on top of that list if you like, but doing one query like this is pretty fast.

How to get a unique set of parent models after querying on child

Order has_many Items is the relationship.
So let's say I have something like the following 2 orders with items in the database:
Order1 {email: alpha#example.com, items_attributes:
[{name: "apple"},
{name: "peach"}]
}
Order2 {email: beta#example.com, items_attributes:
[{name: "apple"},
{name: "apple"}]
}
I'm running queries for Order based on child attributes. So let's say I want the emails of all the orders where they have an Item that's an apple. If I set up the query as so:
orders = Order.joins(:items).where(items: {name:"apple"})
Then the result, because it's pulling at the Item level, will be such that:
orders.count = 3
orders.pluck(:email) = ["alpha#exmaple.com", "beta#example.com", "beta#example.com"]
But my desired outcome is actually to know what unique orders there are (I don't care that beta#example.com has 2 apples, only that they have at least 1), so something like:
orders.count = 2
orders.pluck(:email) = ["alpha#exmaple.com", "beta#example.com"]
How do I do this?
If I do orders.select(:id).distinct, this will fix the problem such that orders.count == 2, BUT this distorts the result (no longer creates AR objects), so that I can't iterate over it. So the below is fine
deduped_orders = orders.select(:id).distinct
deduped_orders.count = 2
deduped_orders.pluck(:email) = ["alpha#exmaple.com", "beta#example.com"]
But then the below does NOT work:
deduped_orders.each do |o|
puts o.email # ActiveModel::MissingAttributeError: missing attribute: email
end
Like I basically want the output of orders, but in a unique way.
I find using subqueries instead of joins a bit cleaner for this sort of thing:
Order.where(id: Item.select(:order_id).where(name: 'apple'))
that ends up with this (more or less) SQL:
select *
from orders
where id in (
select order_id
from items
where name = 'apple'
)
and the in (...) will clear up duplicates for you. Using a subquery also clearly expresses what you want to do–you want the orders that have an item named 'apple'–and the query says exactly that.
use .uniq instead of .distinct
deduped_orders = orders.select(:id).uniq
deduped_orders.count = 2
deduped_orders.pluck(:email) = ["alpha#exmaple.com", "beta#example.com"]
If you want to keep all the attributes of orders use group
deduped_orders = orders.group(:id).distinct
deduped_orders.each do |o|
puts o.email
end
#=> output: "alpha#exmaple.com", "beta#example.com"
I think you just need to remove select(:id)
orders = Order.joins(:items).where(items: {name:"apple"}).distinct
orders.pluck(:email)
# => ["alpha#exmaple.com", "beta#example.com"]
orders = deduped_orders
deduped_orders.each do |o|
puts o.email # loop twice
end

ActiveRecord - Getting the sum of user score

I have an "ITEMS" database made of; ITEM_ID, OWNER_ID and VALUE.
An owner can own unlimited items. An item can only have one owner.
And I have an "OWNER" database made of; ID, NAME
I want to find the NAMES of top 10 RICHEST (wealthier) people. How can I do that?
First, I need to sum the values of an owner_id; than compare that with others?
Here is what you can do :
Item.group(:owner_id) # grouping Items by owner id
.select("SUM(value) as sum") # summing values of each group
.order("sum DESC") # ordering resulting records by the sum value
.limit(10) # giving the top 10 records
It is kinda long solution but it worked for me:
toplist = []
all_owners = Owner.all
all_owners.each do |owner|
name = Owner.find(owner).name
owner_value = Item.where(owner_id: owner).sum(:value)
toplist << [owner_value,name]
end
#top10 = toplist.sort.last(10).reverse

Ruby: how to remove items from array A if it's not in array B?

I have prepare these two arrays:
list_of_students = Student.where('class = ?', param[:given_class])
list_of_teachers = Teacher.where(...)
Student belongs_to Teacher and Teacher has_many students.
And now, I'd need to remove from the list_of_students all items (students), where teacher_id is not included in list_of_teachers.
I found some tips and HOWTO's on comparing arrays, but none of that helped to figure out this case.
Thank you in advance
You can use the IN SQL statement.
list_of_students = Student.where('class = ? AND teacher_id IN (?)', param[:given_class], list_of_teachers.map(&:id))
If the list_of_teachers is an ActiveRecord::Relation (and not an array), you can also use #pluck(:id) or (from Rails 4) #ids
Student.where('class = ? AND teacher_id IN (?)', param[:given_class], list_of_teachers.ids)
There are several ways to write the IN statement. Given you already have a where, I joined it to the main where. But you could also write
Student.where('class = ?', param[:given_class]).where(teacher_id: list_of_teachers)
or
Student.where(class: param[:given_class], teacher_id: list_of_teachers)
Also note you don't need to assign the list of teachers to a variable.
Student.where(class: param[:given_class], teacher_id: Teacher.where(...).ids)
Last but not least, if your Teacher query is simple, you may want to use a single query and a JOIN. Assume you want to get all the Teachers with name Rose.
Student.where(class: param[:given_class], teacher_id: Teacher.where(name: 'Rose').ids)
You can rewrite the same query to
Student.where(class: param[:given_class]).joins(:teacher).where(teacher: { name: 'Rose' })
or (the final and shorter expression)
Student.joins(:teacher).where(class: param[:given_class], teacher: { name: 'Rose' })
You can try something like
a = [1,2,3]
b = [1,4,5]
pry(main)> a.delete_if {|a1| !b.include? a1}
=> [1]
it checks each value in a is in b or not. If not it deletes the value from a and gives you a array finally.
This is an example. You can use this accordingly

Ruby/Rails - Find Foreign Key With Most Instances in Model

I have a joined model that has a foreign key for the model event.
The joined model is called Goals. I'm trying to find the proper find condition to figure out which event_id has the most instances in the Goal join model. Essenially which foreign key id has the most entries in the join model.
Is there a way to do this?
Goal.where(:event.id => ??????? ).first
Couldn't come up with a more elegant solution but try this:
results = Goal.connection.select_all('SELECT COUNT(*) as amount, event_id FROM goals GROUP BY event_id ORDER BY amount DESC LIMIT 0, xx')
raise results.inspect
If you just want the one most event_id with most entries you can also use:
event_id = Goal.connection.select_one('SELECT COUNT(*) as amount, event_id FROM goals GROUP BY event_id ORDER BY amount DESC LIMIT 1').first
If you have set up your models correctly you should be able to do this (if this is not the case then: setup your models correctly):
if Event.all.length == 0
return
end
eventMax = Event.first
Event.all.each do |e|
eventMax = e.goals.length>eventMax.goals.length?e:eventMax
end
#output or do whatever with your newly found event
puts eventMax.to_json
The solution from Danny is not really good.
You should never (or at least very rarely) have to write sql by yourself in rails.

Resources