I am brand new to Rails... I have a question about model initialization. In a simple app that I created, I have two models - "Meeting" and "Organization". In one of my controller methods, I have code like this... def create_meeting @meeting = Meeting.new @organization = Organization.find(1) if (@organization.shortname == ''smallBusiness'') -YwQV3UCrxUlhHtiCqKXthQ@public.gmane.org = ''AM'' end # initialize some defaults @meeting.food = ''coffee" @meeting.location = "room203" @meeting.speaker = "boss" end And off we go to the view... Ok, that works fine. BUT, if I move the @meeting.food, location and speaker into the model initialization like this.. Class Meeting def initialize @food = ''coffee'' @location = ''room 203'' @speaker = ''boss'' end then, the if statement above (specifically the @meeting.time assignment) returns a nil pointer with WEBbrick. I don''t understand what I am doing wrong if I move the defaults into the model constructor... Can anyone help? Shelby
Shelby Westman <shelby.westman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> def create_meeting > @meeting = Meeting.new > @organization = Organization.find(1) > if (@organization.shortname == ''smallBusiness'') > @meeting.time = ''AM'' > end > # initialize some defaults > @meeting.food = ''coffee" > @meeting.location = "room203" > @meeting.speaker = "boss" > end > And off we go to the view... Ok, that works fine. > > BUT, if I move the @meeting.food, location and speaker into the model > initialization like this.. > Class Meeting > def initialize > @food = ''coffee'' > @location = ''room 203'' > @speaker = ''boss'' > end > > then, the if statement above (specifically the @meeting.time > assignment) returns a nil pointer with WEBbrick.@meeting.food isn''t an attribute of Meeting, it''s a method call. My understanding is that ActiveRecord uses MethodMissing to handle the database fields attributes. Here''s the part I don''t get. Inside Class Meeting, if you should just call food or if you need to call self.food. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Hi ! Shelby Westman said the following on 2005-07-14 00:32:> BUT, if I move the @meeting.food, location and speaker into the model > initialization like this.. > Class Meeting > def initialize > @food = ''coffee'' > @location = ''room 203'' > @speaker = ''boss'' > end > > then, the if statement above (specifically the @meeting.time > assignment) returns a nil pointer with WEBbrick. > > I don''t understand what I am doing wrong if I move the defaults into > the model constructor... Can anyone help?It blows up because you never call super to initialize the underlying model. When is the connection to the DB setup ? When are the attributes read, and so on. So, remember to call super, and you''ll be fine. Your initialize method should look like this: def initialize(params=nil) super(params) # Other initialization here self.food = ''coffee'' # etc. end Hope that helps ! François
> It blows up because you never call super to initialize the underlying > model. When is the connection to the DB setup ? When are the > attributes read, and so on. So, remember to call super, and you''ll be fine. > > Your initialize method should look like this: > def initialize(params=nil) > super(params) > # Other initialization here > self.food = ''coffee'' > # etc. > end > >François - Yes, that helps a lot. Thanks! Though that brings up another question - why, if I leave out the initialize block entirely, does the model work? In that case, is there some behind the scenes action that takes care of it?
Shelby Westman wrote:>>It blows up because you never call super to initialize the underlying >>model. When is the connection to the DB setup ? When are the >>attributes read, and so on. So, remember to call super, and you''ll be fine. >> >> >François - Yes, that helps a lot. Thanks! Though that brings up >another question - why, if I leave out the initialize block entirely, >does the model work? In that case, is there some behind the scenes >action that takes care of it? > >If you leave out the initialize it will use the parents initialize method as you haven''t overridden it. As soon as you override it by providing your own initialize method, then you need to call super to do all the things the parents initialize method does. hth -- R.Livsey http://livsey.org