Selecting a record where all associations are present - ruby-on-rails

I have a user that has_many levels. Let's say I have levels 1, 2, 3, and 4.
How do I search for users that have associations with levels 2 AND 3 but not 4?
I haven't gotten for myself. I wanted to focus first on the part before 'but' however the line I came up with:
User.includes(:levels).where(levels: {id: [2, 3]})
returns also users that has only association with level 2, and not only the users with both levels 2 and 3.

Below will work if the order of ids obtained is as [2,3] only. For [3, 2] it will show no records. If the object are in sequence then it should work.
User.includes(:levels).map(&:levels).select { |l| l if l.ids == [2, 3] }

User.includes(:levels).where(levels: {id: 2}).where(levels: {id: 3})

Related

Find model records by column names in the order the array given

I am using mysql. I have to show users record in particular manner which I mentioned as in value fields. I have to order by user_id, not by primary id. I need the objects in the following order: [5, 4, 2, 1, 1, 1].
I am using pluck to view particular column instead of object.
Project.where('user_id IN (?)', [5,4,2,1]).pluck(:user_id)
#=> [1, 1, 1, 2, 4, 5]
But the above query returns [1, 1, 1, 2, 4, 5] instead of [5, 4, 2, 1, 1, 1].
Can anyone help me solve this?
By default, Rails query will sort the records by their ids. If you want to remain the order as the value fields, try to the following code.
user_ids = [5,4,2,1]
Project.where(user_id: user_ids).order("FIELD(user_id, #{user_ids.join(',')})").pluck(:user_id)

Rails query, require all conditions in array

I have two models, both associated with each other through has_many through.
I can query the model and filter based on its associated records:
Car.includes(:equipment).where(equipment: { id: [1, 2, 3] })
The problem is that I want to require all of those records, rather than requiring just one of them.
Is there a way to build a query that requires all of the values in the array (the [1, 2, 3] from the above example).
In other words, I'd like to query for all cars that have all three equipment (ids of 1, 2 and 3).
Assuming you have an id_ary like [1,2,3], how about something like:
id_ary.each_with_object(Car.includes(:equipment)) do |id, scope|
scope.where(equipment: {id: id})
end
Looping like that should and your where conditions, I believe.

A model with "has_and_belongs_to_many" -- how to do a query by a list of id?

I have a model Model1 which has in it:
has_and_belongs_to_many :child_items
Is it possible to query child_items by their id list? Something like:
model1.child_items.where(id: [1, 2, 3])
Yes, I can do this:
ChildItem.where(id: [1, 2, 3])
but via Model1 it'll be better because I there'll be a join and need not just child_items but those that really belong to Model1, and possibly among [1, 2, 3] might other ids as well.
Hence, how can I do that?
model1.child_items.where('child_items.id IN (?)', [1, 2, 3])
I would use this approach instead:
model1_instance.child_items.where(child_items: { id: [1, 2, 3] })
Rails allows you to pass table names as hash keys then add any parameters you want to query on.

Find same values between an array and an active record variable in ruby

Should be an easy one and might have over complicated the title somewhat.
I have a variable that contains records:
#record = Records.all
and an array that holds some task_id's:
#array #has for e.g. [1,2,3]
What i want to do check the task_id column of the list of records in #records to see if they contain any of the numbers in the array. If they do then i want those numbers to be put into another array.
i know this is simple but i keep getting confused along the way as im quite new to ruby syntax.
This should work for you:
#records.map(&:task_id) & #array
This builds the intersection of the two lists (task_ids and array). You can try this example at the console (I hope this helps to clear up how it works):
irb(main):008:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):009:0> b = [3,4,5,6]
=> [3, 4, 5, 6]
irb(main):010:0> a & b
=> [3, 4]

Rails - :order by calculated data

I have multiple instances of data displayed in tables that need to be sorted - much of this data is calculated from the table and isn't just a raw value in the table.
A simple example: Column A = User.visits / User.membership_term
I'm using sortable table columns: http://railscasts.com/episodes/228-sortable-table-columns.
I've tried putting the calculation in the controller and adding a class method to my model but neither seems to work. How can I sort by a calculated field?
You can always use the basic sort method:
irb(main):001:0> a = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> a.sort {|d,e| (d - 3).abs <=> (e - 3).abs}
=> [3, 2, 4, 1, 5] # sort by closest distance away from the number 3
So you can sort your array of active records using similar ways.

Resources