Reputation: 305
I have this query:
case_timeline = Case.select('cases.*, case_steps.*, tasks.*, task_statuses.*')
.joins(case_steps: { tasks: :task_status} )
.where('cases.id = ?', case_1.id)
.where('task_statuses.task_status_name in (?)', ['Open', 'Overdue', 'Completed'])
.order('tasks.end_date DESC')
completed_timeline =
case_timeline
.select{|task| task.task_status_name == "Completed" }
How can I order this by date? I tried this:
completed_timeline =
case_timeline
.select{|task| task.task_status_name == "Completed" }
.order('end_date')
but it's giving the error:
undefined method `order' for #<Array:0x000000051da2e0>
Upvotes: 1
Views: 91
Reputation: 96934
select
with a block converts the ActiveRecord::Relation to an Array; from the docs:
You can pass a block so it can be used just like
Array#select
. This builds an array of objects from the database for the scope, converting them into an array and iterating through them usingArray#select
.
Since task_status_name
appears to be a column, it would be best to do a where
to avoid converting it to an Array entirely:
completed_timeline = case_timeline.where(task_status_name: 'Completed')
.order('end_date')
Alternatively, move the select
call to be after all query method calls.
Upvotes: 0
Reputation: 103
You want the order method on the object called before calling select.
completed_timeline =
case_timeline.order('end_date')
.select{|task| task.task_status_name == "Completed" }
This is due to the fact that case_timeline returns an ActiveRecord object which knows how to respond to order, where calling select returns an Array object which does not know how to respond to order.
Upvotes: 5