Anyone with RoR experience?
Posted: Mon May 22, 2006 5:59 pm
I'm playing with Ruby on Rails, and I'm having trouble understanding what's going wrong and/or how to investigate it.
Here's the two model classes:
When I run things from the console, everything works, for instance
When its run in WEBrick I get "You have a nil object when you didn't expect it! The error occured while evaluating nil.month". The exact same find code is used in both cases, but it appears that the web version doesn't populate the comp_dates field (ie the wrapped event_date (a Date from the DB) is nil)...
Anyone know why? I know I'm doing something wrong, just can't understand what's different between the two environments (console, WEBrick). The "regular" fields in the competition object are being loaded and work in both environments.
Here's the two model classes:
Code: Select all
class CompDate < ActiveRecord::Base
belongs_to :competition
acts_as_list :scope=>:competition_id
def to_formatted_s
event_date.to_formatted_s :long
end
def month
event_date.month
end
def day
event_date.day
end
def year
event_date.year
end
endCode: Select all
class Competition < ActiveRecord::Base
validates_presence_of :name, :short_name, :url
validates_uniqueness_of :short_name
has_many :comp_dates, :order =>:position
def initial_date
comp_dates.first
end
def final_date
comp_dates.last
end
def to_param
short_name
end
def display_dates
i_d = initial_date
if i_d.nil? || i_d.event_date.nil?
reload
end
i_d = initial_date
if i_d==final_date
format=i_d.to_formatted_s
else
i_month = i_d.month
i_year = i_d.year
i_day = i_d.day
f_month = final_date.month
f_day = final_date.day
f_year = final_date.year
format = printf("%s %d-%d, %d",Date::MONTHNAMES[i_month], i_day,f_day,i_year) if i_year==f_year && i_month==f_month
format = printf("%s %d-%s %d, %d",Date::MONTHNAMES[i_month],i_day,Date::MONTHNAMES[f_month],f_day,i_year) if i_year==f_year && i_month!=f_month
format = printf("%s %d, %d - %s %d, %d",Date::MONTHNAMES[i_month],i_day,i_year,Date::MONTHNAMES[f_month],f_day,f_year) if i_year!=f_year
end
return format
end
endCode: Select all
comps = Competition.find(:all, :include=>:comp_dates)
comp(0).display_dates
"April 21-22, 2007"Anyone know why? I know I'm doing something wrong, just can't understand what's different between the two environments (console, WEBrick). The "regular" fields in the competition object are being loaded and work in both environments.