Forum: Ruby on Rails How is this possible?

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.
Eric N. (Guest)
on 2006-05-23 01:49
When run from the console, the following code works, when run through
the WEBrick server I get a "You have a nil object where you didn't
expect it!"

class Competition < ActiveRecord::Base
  has_many :comp_dates, :order=>:position

  def initial_date
    comp_dates.first
  end

  def display_dates
    i_d = initial_date
    if i_d.nil?
      return "unknown"
    else
      return i_d.month
  end
end

i_d should never be nil, in the first place; but how on earth does the
i_d.month calls get reached when i_d is nil?
James L. (Guest)
on 2006-05-23 02:03
(Received via mailing list)
On 5/22/06, Eric N. <removed_email_address@domain.invalid> wrote:
>
> i_d.month calls get reached when i_d is nil?
What makes you think that i_d.month is blowing up?  Wouldn't it be
more likely that comp_dates.first is causing problems?

Do you have some logs to go along with this?  Knowing what you're
doing on your console would help too.

-- James
Eric N. (Guest)
on 2006-05-23 02:20
James L. wrote:
> On 5/22/06, Eric N. <removed_email_address@domain.invalid> wrote:
>>
>> i_d.month calls get reached when i_d is nil?
> What makes you think that i_d.month is blowing up?  Wouldn't it be
> more likely that comp_dates.first is causing problems?
>
> Do you have some logs to go along with this?  Knowing what you're
> doing on your console would help too.
>
> -- James

The trace from the web page:
#{RAILS_ROOT}/app/models/comp_date.rb:10:in `month'
#{RAILS_ROOT}/app/models/competition.rb:25:in `display_dates'
#{RAILS_ROOT}/app/views/register/list.rhtml:10:in
`_run_rhtml_register_list'
#{RAILS_ROOT}/app/views/register/list.rhtml:7:in
`_run_rhtml_register_list'

line 25 in competition.rb is the i_d.month line

the other aspect is the error message:
"You have a nil object when you didn't expect it!
The error occured while evaluating nil.month"
the only place where I'm calling month, is in this function.

From the console: I've done:
comps = Competition.find :all, :include=>:comp_dates
comps(0).display_date

and this generates "real" output and no errors.

The code that the WEBrick server is executing in to reach the error :

class RegisterController < ApplicationController
  def list
    @competitions = Competition.find :all, :include=>:comp_dates
  end

---- IN list.rhtml
<% for competition in @competitions -%>
  <tr>
    <td><%= competition.display_dates -%></td>
  </tr>
<% end -%>
James L. (Guest)
on 2006-05-23 03:20
(Received via mailing list)
On 5/22/06, Eric N. <removed_email_address@domain.invalid> wrote:
> > -- James
>
> The trace from the web page:
> #{RAILS_ROOT}/app/models/comp_date.rb:10:in `month'

What's going on at line 10 in this file?

-- James
Eric N. (Guest)
on 2006-05-23 03:57
James L. wrote:
> On 5/22/06, Eric N. <removed_email_address@domain.invalid> wrote:
>> > -- James
>>
>> The trace from the web page:
>> #{RAILS_ROOT}/app/models/comp_date.rb:10:in `month'
>
> What's going on at line 10 in this file?
>
> -- James
Ahh, now why wasn't I looking there... I should know how to read a stack
trace

def month
  event_date.moth
end


so if I'm reading this right, this means that the list container is
being created properly and there is a first element, but the CompDate
inside that first element isn't beling loaded from the DB so the
internal event_date is nil  (hence i_d is not nul, but i_d.event_date is
nil)

How would I go about figuring out why the event_data isn't getting
loaded?  What piece of the puzzle am I missing in how the data is
plumbed through rails?
Ben and Kaz A. (Guest)
on 2006-05-23 04:16
(Received via mailing list)
On 5/23/06, Eric N. <removed_email_address@domain.invalid> wrote:
> def month
>   event_date.moth
> end

Assuming you've copied and pasted this from your code, could it be
that you've spelt "month" as "moth".

cheers,
Ben
Eric N. (Guest)
on 2006-05-23 04:19
Ben and Kaz A. wrote:
> On 5/23/06, Eric N. <removed_email_address@domain.invalid> wrote:
>> def month
>>   event_date.moth
>> end
>
> Assuming you've copied and pasted this from your code, could it be
> that you've spelt "month" as "moth".
>
> cheers,
> Ben

No, it was a typo, thought the code was short enough to re-type and not
cut-paste....  Its correct in the source.
Ben and Kaz A. (Guest)
on 2006-05-23 05:13
(Received via mailing list)
On 5/23/06, Eric N. <removed_email_address@domain.invalid> wrote:
> No, it was a typo, thought the code was short enough to re-type and not
> cut-paste....  Its correct in the source.

Ok.  Have you checked ./log/development.log to see the SQL used by
"Competition.find :all, :include=>:comp_dates" when called from your
controller?  Does it look sane?  It's odd that it works one way from
the console and another way in your app.

cheers,
Ben
Eric N. (Guest)
on 2006-05-23 05:13
I've been staring at the logs trying to see if that lets me figure any
out as to why the console versus WEBrick behavoir is different and I've
found something that confuses me:

In the console I enter
comps = Competition.find :all :include=>:comp_dates
comps[0].display_dates

and in the log I see a single query with a header = "Competition Load
Including Associations" and no further activty

When I hit the webpage that does a
@competitions = Competition.find :all :include=>:comp_dates

I see the same query.  However when it reaches the
rendering register/list section of the log (ie when it starts to loop
over the @competitions array)
I see two new queries Competition Load and CompDate Load.  So the
application is following the 1+2n pattern while the console does a
single query.

Right after the CompDate Load, I get the nil error that I've been
chasing this whole thread, I've cut and pasted the queries into the DB
and they return the correct values.  Somehow it appears that the initial
"Competition Load Including Associations" isn't correctly populating the
data and/or signalling that its been loaded or something?

Where else should I be looking, I feel like I'm chasing the wrong
sympton, but I don't know where else to look.
Ben and Kaz A. (Guest)
on 2006-05-23 05:29
(Received via mailing list)
On 5/23/06, Eric N. <removed_email_address@domain.invalid> wrote:
> When I hit the webpage that does a
> @competitions = Competition.find :all :include=>:comp_dates

If you put a breakpoint() in your controller after the above line and
check @competitions[0].display_dates does it return what you're
expecting?

cheers,
Ben
Eric N. (Guest)
on 2006-05-23 07:10
Ben and Kaz A. wrote:
> On 5/23/06, Eric N. <removed_email_address@domain.invalid> wrote:
>> When I hit the webpage that does a
>> @competitions = Competition.find :all :include=>:comp_dates
>
> If you put a breakpoint() in your controller after the above line and
> check @competitions[0].display_dates does it return what you're
> expecting?
>
> cheers,
> Ben

No it gives the same error.  Stepping through the data structure reveals
something unusual....

@competitions ----> looks good
@competitions[0] ----> looks good
@competitions[0].comp_dates --> looks good
@competitions[0].comp_dates[0] --> looks good :
#<CompDate:0x2778e80 @attributes={"id"=>"1", "event_date"=>"2007-04-22",
"position"=>"1", "competition_id"=>"5"}>

@competitions[0].comp_dates[0].event_date -- > nil .... not so good

In the console I get:
>> comps[0].comp_dates[0]
=> #<CompDate:0x2597060 @attributes={"id"=>"1",
"event_date"=>"2007-04-22", "position"=>"1", "competition_id"=>"5"}>
>> comps[0].comp_dates[0].event_date
=> #<Date: 4908425/2,0,2299161>

In both cases the @competitions/comps variable were populated with the
same Competition.find command.
Ben and Kaz A. (Guest)
on 2006-05-23 07:19
(Received via mailing list)
On 5/23/06, Eric N. <removed_email_address@domain.invalid> wrote:
> @competitions[0].comp_dates[0].event_date -- > nil .... not so good
I'm clutching at straws here and I don't know why the behaviour's
different in the console.   But, what does your CompDate model look
like?  Are you able to paste the model in here or at
http://rafb.net/paste/ ?

cheers,
Ben
Eric N. (Guest)
on 2006-05-23 07:35
Ben and Kaz A. wrote:
> On 5/23/06, Eric N. <removed_email_address@domain.invalid> wrote:
>> @competitions[0].comp_dates[0].event_date -- > nil .... not so good
> I'm clutching at straws here and I don't know why the behaviour's
> different in the console.   But, what does your CompDate model look
> like?  Are you able to paste the model in here or at
> http://rafb.net/paste/ ?
>
> cheers,
> Ben

On a whim, I restarted WEBrick, and it works now...  I know I was doing
it regularly all along, but it didn't fix anything before. Not sure why
it fixed it now, but I won't complain.

Thanks for all the help and time.
Ben and Kaz A. (Guest)
on 2006-05-23 07:38
(Received via mailing list)
On 5/23/06, Eric N. <removed_email_address@domain.invalid> wrote:
> Thanks for all the help and time.

No worries.
This topic is locked and can not be replied to.