class AppointmentBook < ActiveRecord::Base
has_many :appointments
def lookup(date, look_ahead = 27)
return nil unless block_given?
(date..date + look_ahead).each do |date|
yield(date, appointments.find_all_by_date(date))
end
end
end
The line with yield is throwing the error:
undefined method 'each' for #<Date: 4908761/2,0,2299161>
What do you EXPECT this to do?
Assuming that date is a Date then date+lookahead is also going to be a Date.
Dates aren't enumerable, and don't have an each method.
If you want this to yield each appointment with a date look_ahead days
from date, then you probably want something like:
def lookup(date, look_ahead = 27)
return nil unless block_given?
appointments.find_all_by_date(date..date + look_ahead).each do
appointment>
yield appointment
end
end
The kicker: if I call #to_s on date inside my method, it works. But I
shouldn't have to call #to_s.
to_s will return a string representation of the date. Strings DO have
an each method which when called without an argument yields each "\n"
delimited line in the string.
Sorry I missed the two dots.
You said it was the line with the yield which is raising the
exception, but there's no each there.
So...
It would seem to be something called by that line.
Others are looking at the find_all_by date, but what about the block
you are yielding to? What's in that block?
So what line in what file is actually suffering the method missing?
Seems to be time to take advantage of the fact that ActiveRecord and
the rest of Rails is open source.
Try starting a new project and just build up a little bit. I did a
new project that used that column name and type, and no error. So I
thought maybe it was a problem with the association, but that still
worked fine. Basically I'd put in the absolutely simplest stuff, and
build up to the same structure that you currently have, and see where
it breaks along the way.
Pat
I find that looking at the source is usually the quickest way to
figure these kind of things out.
The same debugging techniques that you use for your own code work for
library code.
Plus there's the benefit that you often learn useful things in looking
at the code.