Forum: Ruby on Rails to_date method problems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Charley M. (Guest)
on 2008-11-21 23:58
I'm having problems, all of a sudden, with some code that's been working
for years.  Someone else wrote this code, and I'm just trying to
maintain it - I don't have expert experience with ruby or rails.  The
code grabs all invoices processed within the past week for a customer
and send an email to the appropriate person, like this:

  # finds the most recent invoices for this organization
  def find_recent_invoices(number=6)
    date = (Time.now - number.days).to_date
    invoices.find(:all, :order => 'posted_on asc', :conditions =>
["invoices.posted_on >= ?", date])
  end

I get this error:

NoMethodError (private method `to_date' called for Sat Nov 15 13:32:51
-0800 2008:Time):
    /app/models/organization.rb:67:in `find_recent_invoices'
    /app/controllers/admin/communicate_controller.rb:96:in
`send_invoices'
    /app/controllers/admin/communicate_controller.rb:95:in `each'
    /app/controllers/admin/communicate_controller.rb:95:in
`send_invoices'
    /vendor/rails/actionpack/lib/action_controller/base.rb:849:in `send'
    /vendor/rails/actionpack/lib/action_controller/base.rb:849:in
`perform_action_without_filters'
    /vendor/rails/actionpack/lib/action_controller/filters.rb:332:in
`perform_action_without_benchmark'
    /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
    /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
    /vendor/rails/actionpack/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
    /vendor/rails/actionpack/lib/action_controller/rescue.rb:82:in
`perform_action'
    /vendor/rails/actionpack/lib/action_controller/base.rb:369:in `send'
    /vendor/rails/actionpack/lib/action_controller/base.rb:369:in
`process_without_session_management_support'
    /vendor/rails/actionpack/lib/action_controller/session_management.rb:116:in
`process'
    /vendor/rails/railties/lib/dispatcher.rb:38:in `dispatch'
    /lib/fcgi_handler.rb:136:in `process_request'
    /lib/fcgi_handler.rb:62:in `process!'
    /usr/lib/ruby/1.8/fcgi.rb:612:in `each_cgi'
    /usr/lib/ruby/1.8/fcgi.rb:609:in `each'
    /usr/lib/ruby/1.8/fcgi.rb:609:in `each_cgi'
    /lib/fcgi_handler.rb:53:in `process!'
    /lib/fcgi_handler.rb:20:in `process!'
    dispatch.fcgi:24
Philip H. (Guest)
on 2008-11-22 00:33
(Received via mailing list)
On Nov 21, 2008, at 1:58 PM, Charley M. wrote:

>    date = (Time.now - number.days).to_date
>    invoices.find(:all, :order => 'posted_on asc', :conditions =>
> ["invoices.posted_on >= ?", date])
>  end
>
> I get this error:
>
> NoMethodError (private method `to_date' called for Sat Nov 15 13:32:51
> -0800 2008:Time):

I don't remember the particulars, but you've hit the mismatched ruby/
rails bug that came up about rails 1.2.3 I think it was.  Maybe
1.2.6.  Anyway, at one point some methods went private in ruby that
Rails used.  Or vice versa.  Anyway, the next version of rails put
them back because people were used to them.  Google around for
specifics.

In any event, I suspect you've either just recently updated Ruby or
Rails when this problem started.

If it helps, the line giving you problems works for me with rails
2.1.0 and ruby 1.8.6 (2008-03-03 patchlevel 114).

Change the code to this and it should work for you:

def find_recent_invoices(number=6)
   invoices.find(:all, :order => 'posted_on asc',
                 :conditions => ["invoices.posted_on >= ?",
number.days.ago])
end


-philip
Charley M. (Guest)
on 2008-11-22 07:13
Thanks for the help - you're a life saver.  I'll be sure to let our
programmer know he lost me my Friday night.  ;)

> Change the code to this and it should work for you:
>
> def find_recent_invoices(number=6)
>    invoices.find(:all, :order => 'posted_on asc',
>                  :conditions => ["invoices.posted_on >= ?",
> number.days.ago])
> end
>
>
> -philip
This topic is locked and can not be replied to.