I figure ranges should work just fine for a datetime. I want to weight a total based on how recently an item was created. Using the case statement, I check various ranges. I simply cannot get this to work: weighted_total + case item.created_at when Time.now...24.hours.ago: 20 when 24.hours.ago...48.hours.ago: 8 when 48.hours.ago...1.week.ago: 4 when 1.week.ago...2.weeks.ago: 2 when 2.weeks.ago...4.weeks.ago: 1 else 0 end Every "created_at" field resolve to the else case. I also tried it with if statements and Time.now...24.hours.ago ==item.created at boolean expressions. In that case, every single if statement was successful, regardless of the range. I''m sure I''m simply missing some intricacy with date handling. Every language seems to have its gotchas in this realm. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bjhess wrote:> I figure ranges should work just fine for a datetime. I want to weight > a total based on how recently an item was created. Using the case > statement, I check various ranges. I simply cannot get this to work: > > weighted_total +> case item.created_at > when Time.now...24.hours.ago: 20 > when 24.hours.ago...48.hours.ago: 8 > when 48.hours.ago...1.week.ago: 4 > when 1.week.ago...2.weeks.ago: 2 > when 2.weeks.ago...4.weeks.ago: 1 > else 0 > end > > Every "created_at" field resolve to the else case. > > I also tried it with if statements and Time.now...24.hours.ago ==> item.created at boolean expressions. In that case, every single if > statement was successful, regardless of the range. > > I''m sure I''m simply missing some intricacy with date handling. Every > language seems to have its gotchas in this realm.Try reversing the order of your ranges so that the oldest time is defined first... case item.created_at when 24.hours.ago ... Time.now: 20 when 48.hours.ago ... 24.hours.ago: 4 ... end _Kevin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, _Kevin. You''re a life saver! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---