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? -- Posted via http://www.ruby-forum.com/.
On 5/22/06, Eric Nielsen <nielsene@mit.edu> wrote:> 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?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
James Ludlow wrote:> On 5/22/06, Eric Nielsen <nielsene@mit.edu> 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. > > -- JamesThe 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 -%> -- Posted via http://www.ruby-forum.com/.
On 5/22/06, Eric Nielsen <nielsene@mit.edu> wrote:> James Ludlow wrote: > > On 5/22/06, Eric Nielsen <nielsene@mit.edu> 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''What''s going on at line 10 in this file? -- James
James Ludlow wrote:> On 5/22/06, Eric Nielsen <nielsene@mit.edu> 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? > > -- JamesAhh, 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? -- Posted via http://www.ruby-forum.com/.
On 5/23/06, Eric Nielsen <nielsene@mit.edu> wrote:> def month > event_date.moth > endAssuming you''ve copied and pasted this from your code, could it be that you''ve spelt "month" as "moth". cheers, Ben
Ben and Kaz Askins wrote:> On 5/23/06, Eric Nielsen <nielsene@mit.edu> 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, > BenNo, it was a typo, thought the code was short enough to re-type and not cut-paste.... Its correct in the source. -- Posted via http://www.ruby-forum.com/.
On 5/23/06, Eric Nielsen <nielsene@mit.edu> 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
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. -- Posted via http://www.ruby-forum.com/.
On 5/23/06, Eric Nielsen <nielsene@mit.edu> wrote:> When I hit the webpage that does a > @competitions = Competition.find :all :include=>:comp_datesIf 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
Ben and Kaz Askins wrote:> On 5/23/06, Eric Nielsen <nielsene@mit.edu> 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, > BenNo 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. -- Posted via http://www.ruby-forum.com/.
On 5/23/06, Eric Nielsen <nielsene@mit.edu> wrote:> 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 goodI''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
Ben and Kaz Askins wrote:> On 5/23/06, Eric Nielsen <nielsene@mit.edu> 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, > BenOn 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. -- Posted via http://www.ruby-forum.com/.