I''m selecting data into a controller and sorting it. The code (in the controller) looks like this: def index @assignments = [] tasks = Task.find(:all, :conditions => ["person_id = ? and actual_end_date is null", session[:user].id.to_s ]) issues = Issue.find(:all, :conditions => ["person_id = ? and status <> ''closed''" , session[:user].id.to_s] ) @assignments.concat(tasks) @assignments.concat(issues) @assignments= @assignments.sort @assignments.each {|ea| puts ea.name + ea.scheduled_end_date.to_s} end The output from the puts statement is this: P2 Task1 2006-03-16 T2 2006-03-23 Test Task 2006-01-05 super tests 2006-01-19 super test 3 2006-01-26 Larry''s issue 2006-03-09 For some reason, the sort algorithm thinks that all the dates are nil so they don''t get sorted, but they''re there as the puts shows. The sort method is below. When i didn''t include the check for nil in it, it threw an exception, saying nil doesn''t understand <=>. # Sort by due date (scheduled_end_date) def <=> aTask @scheduled_end_date.nil? ? -1 : @scheduled_end_date <=> aTask.scheduled_end_date end Any ideas would be greatly appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060309/987e777f/attachment.html
@assignments= @assignments.sort {|a,b| a.scheduled_end_date <=> b.scheduled_end_date} On 3/9/06, Larry White <ljw1001@gmail.com> wrote:> I''m selecting data into a controller and sorting it. The code (in the > controller) looks like this: > > def index > @assignments = [] > tasks = Task.find(:all, :conditions => ["person_id = ? and > actual_end_date is null", session[:user].id.to_s ]) > issues = Issue.find(:all, :conditions => ["person_id = ? and > status <> ''closed''" , session[:user].id.to_s] ) > @assignments.concat(tasks) > @assignments.concat(issues) > @assignments= @assignments.sort > @assignments.each {|ea| puts ea.name + ea.scheduled_end_date.to_s} > end > > The output from the puts statement is this: > P2 Task1 2006-03-16 > T2 2006-03-23 > Test Task 2006-01-05 > super tests 2006-01-19 > super test 3 2006-01-26 > Larry''s issue 2006-03-09 > > For some reason, the sort algorithm thinks that all the dates are nil so > they don''t get sorted, but they''re there as the puts shows. The sort method > is below. When i didn''t include the check for nil in it, it threw an > exception, saying nil doesn''t understand <=>. > > # Sort by due date (scheduled_end_date) > def <=> aTask > @scheduled_end_date.nil? ? -1 : @scheduled_end_date <=> > aTask.scheduled_end_date > end > > Any ideas would be greatly appreciated. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Hi Larry, Try this: tasks = Task.find_by_person_id session[:user].id, :order => ''scheduled_end_date DESC'' # or ASC for ascending Hope that helps (you may want to fire up the console using ''script/ console'' and try out diffrent queries) Matt On Mar 9, 2006, at 4:36 PM, Larry White wrote:> I''m selecting data into a controller and sorting it. The code (in > the controller) looks like this: > > def index > @assignments = [] > tasks = Task.find(:all, :conditions => ["person_id = ? > and actual_end_date is null", session[:user].id.to_s ]) > issues = Issue.find(:all, :conditions => ["person_id = ? > and status <> ''closed''" , session[:user].id.to_s] ) > @assignments.concat(tasks) > @assignments.concat(issues) > @assignments= @assignments.sort > @assignments.each {|ea| puts ea.name + > ea.scheduled_end_date.to_s} > end > > The output from the puts statement is this: > P2 Task1 2006-03-16 > T2 2006-03-23 > Test Task 2006-01-05 > super tests 2006-01-19 > super test 3 2006-01-26 > Larry''s issue 2006-03-09 > > For some reason, the sort algorithm thinks that all the dates are > nil so they don''t get sorted, but they''re there as the puts shows. > The sort method is below. When i didn''t include the check for nil > in it, it threw an exception, saying nil doesn''t understand <=>. > > # Sort by due date (scheduled_end_date) > def <=> aTask > @scheduled_end_date.nil? ? -1 : @scheduled_end_date <=> > aTask.scheduled_end_date > end > > Any ideas would be greatly appreciated. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
that works. Thank you. But Why? I thought the two approaches were equivilent. On 3/9/06, Lugovoi Nikolai <meadow.nnick@gmail.com> wrote:> > @assignments= @assignments.sort {|a,b| a.scheduled_end_date <=> > b.scheduled_end_date} > > > On 3/9/06, Larry White <ljw1001@gmail.com> wrote: > > I''m selecting data into a controller and sorting it. The code (in the > > controller) looks like this: > > > > def index > > @assignments = [] > > tasks = Task.find(:all, :conditions => ["person_id = ? and > > actual_end_date is null", session[:user].id.to_s ]) > > issues = Issue.find(:all, :conditions => ["person_id = ? and > > status <> ''closed''" , session[:user].id.to_s] ) > > @assignments.concat(tasks) > > @assignments.concat(issues) > > @assignments= @assignments.sort > > @assignments.each {|ea| puts ea.name + > ea.scheduled_end_date.to_s} > > end > > > > The output from the puts statement is this: > > P2 Task1 2006-03-16 > > T2 2006-03-23 > > Test Task 2006-01-05 > > super tests 2006-01-19 > > super test 3 2006-01-26 > > Larry''s issue 2006-03-09 > > > > For some reason, the sort algorithm thinks that all the dates are nil > so > > they don''t get sorted, but they''re there as the puts shows. The sort > method > > is below. When i didn''t include the check for nil in it, it threw an > > exception, saying nil doesn''t understand <=>. > > > > # Sort by due date (scheduled_end_date) > > def <=> aTask > > @scheduled_end_date.nil? ? -1 : @scheduled_end_date <=> > > aTask.scheduled_end_date > > end > > > > Any ideas would be greatly appreciated. > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060309/e77195eb/attachment.html
I need to concatenate the results from two tables before sorting, so this doesn''t work. On 3/9/06, Matthias von Rohr <mvonrohr@besonet.ch> wrote:> > Hi Larry, > > Try this: > > tasks = Task.find_by_person_id session[:user].id, :order => > ''scheduled_end_date DESC'' # or ASC for ascending > > Hope that helps (you may want to fire up the console using ''script/ > console'' and try out diffrent queries) > > Matt > > On Mar 9, 2006, at 4:36 PM, Larry White wrote: > > > I''m selecting data into a controller and sorting it. The code (in > > the controller) looks like this: > > > > def index > > @assignments = [] > > tasks = Task.find(:all, :conditions => ["person_id = ? > > and actual_end_date is null", session[:user].id.to_s ]) > > issues = Issue.find(:all, :conditions => ["person_id = ? > > and status <> ''closed''" , session[:user].id.to_s] ) > > @assignments.concat(tasks) > > @assignments.concat(issues) > > @assignments= @assignments.sort > > @assignments.each {|ea| puts ea.name + > > ea.scheduled_end_date.to_s} > > end > > > > The output from the puts statement is this: > > P2 Task1 2006-03-16 > > T2 2006-03-23 > > Test Task 2006-01-05 > > super tests 2006-01-19 > > super test 3 2006-01-26 > > Larry''s issue 2006-03-09 > > > > For some reason, the sort algorithm thinks that all the dates are > > nil so they don''t get sorted, but they''re there as the puts shows. > > The sort method is below. When i didn''t include the check for nil > > in it, it threw an exception, saying nil doesn''t understand <=>. > > > > # Sort by due date (scheduled_end_date) > > def <=> aTask > > @scheduled_end_date.nil? ? -1 : @scheduled_end_date <=> > > aTask.scheduled_end_date > > end > > > > Any ideas would be greatly appreciated. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060309/8218ba4a/attachment.html