I''m trying to compare two dates (one from a database, and the other is the current date). Here is the code I''m using: ------ if document.nil? "doc_none" elsif document.status == ''archived'' "doc_archived" elsif document.expiration_on < 720.days.ago "doc_expired_720" elsif document.expiration_on < 360.days.ago "doc_expired_360" elsif document.expiration_on < 90.days.ago "doc_expired_90" elsif document.expiration_on < 60.days.ago "doc_expired_60" elsif document.expiration_on < 30.days.ago "doc_expired_30" elsif document.expiration_on < 0.days.ago "doc_expired" elsif document.expiration_on < 30.days.from.now "doc_expiring_30" elsif document.expiration_on < 60.days.from.now "doc_expiring_60" else "doc_current" end ------ Here is the error in my log: ------ ActionView::TemplateError (comparison of Date with Time failed) on line #46 of app/views/admin/documents/list.rhtml: 43: <tr> 44: <td><%= link_to document.expiration_on.strftime("%Y.%m.%d"), :action => ''show'', :id => document %></td> 45: <td><%= link_to_organization(document.organization) %></td> 46: <td class="<%= expiration_class(document) %>"> 47: <%=hh document.status_readable %> </td> 48: <td><%= link_to_doctype(document.doctype) %></td> 49: <td><%= link_to document.service_date.strftime("%Y.%m.%d"), :action => ''show'', :id => document %></td> #{RAILS_ROOT}/app/helpers/organizations_helper.rb:12:in `<'' #{RAILS_ROOT}/app/helpers/organizations_helper.rb:12:in `expiration_class'' #{RAILS_ROOT}/app/views/admin/documents/list.rhtml:46 #{RAILS_ROOT}/app/views/admin/documents/list.rhtml:42:in `each'' #{RAILS_ROOT}/app/views/admin/documents/list.rhtml:42 ------ It sounds like my dates aren''t in the same format, but I''m not sure what the solution is. I''m using mySQL, and the field that I''m grabbing the data from is of type "date". -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 24 Nov 2008, at 16:35, Charley Mills wrote:> > I''m trying to compare two dates (one from a database, and the other is > the current date). Here is the code I''m using: > > ------ > if document.nil? > "doc_none" > elsif document.status == ''archived'' > "doc_archived" > elsif document.expiration_on < 720.days.ago > "doc_expired_720" > elsif document.expiration_on < 360.days.ago > "doc_expired_360" > elsif document.expiration_on < 90.days.ago > "doc_expired_90" > elsif document.expiration_on < 60.days.ago > "doc_expired_60" > elsif document.expiration_on < 30.days.ago > "doc_expired_30" > elsif document.expiration_on < 0.days.ago > "doc_expired" > elsif document.expiration_on < 30.days.from.now > "doc_expiring_30" > elsif document.expiration_on < 60.days.from.now > "doc_expiring_60" > else > "doc_current" > end > ------ >So ruby has several classes to do with dates and times. - Time which is implemented as a number of seconds since an epoch (usually a 32bit which restricts the range of representable dates) - Date which is a number of days (with all the niceties to do with Julian reform, Gregorian reform etc...) - DateTime which is sort of like Date but which also has time of day info date columns on the database comes back as instances of Date, but 0.days.ago etc.. will be instances of Time. You can convert these around with to_date/to_s. I might write this as delta = Date.today - document.expiration_on case delta when 720..360 then ''doc_expired_360'' when 360..90 then ''doc_expired_90'' etc... Fred> Here is the error in my log: > > ------ > ActionView::TemplateError (comparison of Date with Time failed) on > line > #46 of app/views/admin/documents/list.rhtml: > 43: <tr> > 44: <td><%= link_to > document.expiration_on.strftime("%Y.%m.%d"), :action => ''show'', :id => > document %></td> > 45: <td><%= link_to_organization(document.organization) > %></td> > 46: <td class="<%= expiration_class(document) %>"> > 47: <%=hh document.status_readable > %> </td> > 48: <td><%= link_to_doctype(document.doctype) %></td> > 49: <td><%= link_to document.service_date.strftime("%Y.%m. > %d"), > :action => ''show'', :id => document %></td> > > #{RAILS_ROOT}/app/helpers/organizations_helper.rb:12:in `<'' > #{RAILS_ROOT}/app/helpers/organizations_helper.rb:12:in > `expiration_class'' > #{RAILS_ROOT}/app/views/admin/documents/list.rhtml:46 > #{RAILS_ROOT}/app/views/admin/documents/list.rhtml:42:in `each'' > #{RAILS_ROOT}/app/views/admin/documents/list.rhtml:42 > ------ > > It sounds like my dates aren''t in the same format, but I''m not sure > what > the solution is. I''m using mySQL, and the field that I''m grabbing the > data from is of type "date". > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''ve written it as this: ---- def expiration_class(document) delta = Date.today - document.expiration_on result = case delta when 720..360: "doc_expired_360" when 360..90: "doc_expired_90" when 90..60: "doc_expired_60" when 60..30: "doc_expired_30" when 30..1: "doc_expired" else ''doc_current'' end end end ---- The problem, though, is that it''s returning all deltas as "doc_current", when I know they''re not. Any ideas? Frederick Cheung wrote:> I might write this as > delta = Date.today - document.expiration_on > case delta > when 720..360 then ''doc_expired_360'' > when 360..90 then ''doc_expired_90'' > > etc... > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 24 Nov 2008, at 18:40, Charley Mills wrote:> > I''ve written it as this: > > ---- > def expiration_class(document) > > delta = Date.today - document.expiration_on > > result = case delta > when 720..360: "doc_expired_360" > when 360..90: "doc_expired_90" > when 90..60: "doc_expired_60" > when 60..30: "doc_expired_30" > when 30..1: "doc_expired" > else ''doc_current'' > end > > end > end > ---- > > The problem, though, is that it''s returning all deltas as > "doc_current", > when I know they''re not. Any ideas? >because your ranges are back to front. ranges must be smallest..largest (check whether you want ... instead of .. i never remember which is which) Fred> > Frederick Cheung wrote: >> I might write this as >> delta = Date.today - document.expiration_on >> case delta >> when 720..360 then ''doc_expired_360'' >> when 360..90 then ''doc_expired_90'' >> >> etc... >> >> Fred > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---