Sorting with data from another table using sort_by ruby on rails -


i have 2 models: todo , duedate. duedate has_many todos, , likewise todo belongs_to duedate. duedates table holds id , 'date' data type entry. every todo object has duedate_id column corresponds duedate id.

todo has other values in priority , completed, sort by:

@todos = @todos.sort_by(&:priority) 

i want sort @todos date, i'm not sure how tell sort_by use date associated duedate_id in duedate table. can't do

@todos = @todos.sort_by(&:duedate_id) 

because sort @todos date entered duedate table, not date entry corresponds to. can help?

thanks, ross

you're using symbol_to_proc sugar method. can use plain-old block argument sort_by:

@todos = @todos.sort_by{ |todo| todo.duedate.date } 

Comments

Popular posts from this blog

jQuery clickable div with working mailto link inside -

java - Getting corefrences with Standard corenlp package -

WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl -