Wes Gamble
2006-Jul-06 21:15 UTC
[Rails] Custom init. of ActiveRecord objects - best practices
All, I''m wanting to initialize an ActiveRecord object. I understand that there is a method called after_initialize that appears to get called right after the ActiveRecord object is instantiated. Is after_initialize a Ruby thing or a Rails thing? Where is after_initialize documented? Can I pass parameters to it? If so, how? I want to initialize the "belongs_to" attribute of my new ActiveRecord object but can''t figure out how to do it. Should I just write a standalone public (!) method that will take whatever parameters I need to set on my new object and then call this method after my call to Object.new? (I''m not calling Object.create so before_save is not useful to me here). In general, what is the best way to perform custom initialization on an ActiveRecord object? Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-06 21:28 UTC
[Rails] Re: Custom init. of ActiveRecord objects - best practices
Wes Gamble wrote:> All, > > I''m wanting to initialize an ActiveRecord object. > > I understand that there is a method called after_initialize that appears > to get called right after the ActiveRecord object is instantiated. > > Is after_initialize a Ruby thing or a Rails thing? > Where is after_initialize documented? > Can I pass parameters to it? If so, how? > > I want to initialize the "belongs_to" attribute of my new ActiveRecord > object but can''t figure out how to do it. > > Should I just write a standalone public (!) method that will take > whatever parameters I need to set on my new object and then call this > method after my call to Object.new? (I''m not calling Object.create so > before_save is not useful to me here). > > In general, what is the best way to perform custom initialization on an > ActiveRecord object? > > Thanks, > WesI suppose that if I want to initialize certain fields to default values, I have the option of putting login into the appropriate getters to handle that, but that seems awfully kludgy. Wes -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-06 21:53 UTC
[Rails] Re: Custom init. of ActiveRecord objects - best practices
Based on another post (http://www.ruby-forum.com/topic/13701#4699), I think I understand how to handle this. In order to set the attributes of the job to something custom, you need to define def initialize(attributes) super(attributes) end where attributes is a hash keyed by attribute name containing the default values that you want. Any further attribute initialization should happen here. Any initialization of _instance variables_ should probably go into the after_initialize method (although it seems like you could include that in the overridden initialize method as well). there''s no mention of after_initialize in the API docs. so I''m not sure if it''s ok to use or not. Here''s my example - I have two models, Job and Contact. I want to initialize Job and set it''s Contact to be the current user. Note that I''m not saving my job yet (so using Job.new, not Job.create). class Job < ActiveRecord::Base belongs_to :contact, :foreign_key => ''ContactNumber'' class Contact < ActiveRecord::Base has_many :jobs, :foreign_key => ''ContactNumber'' In my controller, I say: @current_job = Job.new({ :contact => Contact.find(session[:user]) }) In job.rb, I override the initialize method: public def initialize(attributes = nil) super(attributes) self.FAX = contact.FAX self.EMAIL = contact.EMAIL end This seems to work. In my case, I didn''t need to do any initialization except for attributes so I didn''t define the after_initialize method. Wes -- Posted via http://www.ruby-forum.com/.