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.
Related
I need to separate the elements of a single collection by some predicate
into two collections, one containing the YES-elements, the other containing to NO-elements.
I want to do that in only one iteration and I want to avoid temporary copies.
The following works for me:
irb> aaa = [ 1, 2, 3, 4 ,5, 6, 7, 8, 9 ]; bbb = [];
irb> aaa.reject!{|n| bbb<<n unless n.odd? }
irb> bbb
# => [2, 4, 6, 8]
irb> aaa
# => [1, 3, 5, 7, 9]
but I wonder, is there a better more readable solution AND
is there something like this for a Hash?
Update: see this Ruby: how to split a collection (Enumerable) in multiple (>2) partitions by some predicate?
Use Enumerable#partition:
partition { |obj| block } → [ true_array, false_array ]
partition → an_enumerator
Returns two arrays, the first containing the elements of enum for
which the block evaluates to true, the second containing the rest.
irb(main):011:0> (1..10).partition(&:odd?)
=> [[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]]
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)
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})
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.
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]