Jonathan Leonard
2005-Nov-25 04:11 UTC
overriding initialize or new for a subclass of ActiveRecord
Hi, I''m trying to override ''initialize'' for a subclass of ActiveRecord::Base (a model) and the code isn''t getting hit. I''ve also tried overriding ''new'' to no avail. Is there a trick to this? I''m fairly new to Ruby and Rails so it is probably something simple. Thanks, Jonathan -- Posted via http://www.ruby-forum.com/.
John Meredith
2005-Nov-25 04:16 UTC
Re: overriding initialize or new for a subclass of ActiveRecord
Hi Jonathan, Make sure you call super ie. def initialize super # ... do stuff ... end - John On 25/11/2005, at 2:11 PM, Jonathan Leonard wrote:> Hi, > > I''m trying to override ''initialize'' for a subclass of > ActiveRecord::Base > (a model) and the code isn''t getting hit. I''ve also tried overriding > ''new'' to no avail. Is there a trick to this? I''m fairly new to Ruby > and Rails so it is probably something simple. > > Thanks, > Jonathan > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- John Meredith <jmeredith-0iPedL6B8ETQT0dZR+AlfA@public.gmane.org>
Jonathan Leonard
2005-Nov-25 04:29 UTC
Re: overriding initialize or new for a subclass of ActiveRec
Thanks for the answer, but I tried that as well. I put a breakpoint in the func like so: def initialize breakpoint super # other stuff end and the breakpoint never gets hit. --Jonathan john wrote:> Hi Jonathan, > > Make sure you call super ie. > > def initialize > super > > # ... do stuff ... > end > > - John > > On 25/11/2005, at 2:11 PM, Jonathan Leonard wrote: > >> >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > -- > John Meredith <jmeredith-0iPedL6B8ETQT0dZR+AlfA@public.gmane.org>-- Posted via http://www.ruby-forum.com/.
Tudor Oprea
2005-Nov-25 08:15 UTC
Re: overriding initialize or new for a subclass of ActiveRec
johanatan wrote:> Thanks for the answer, but I tried that as well. I put a breakpoint in > the func like so: > > def initialize > breakpoint > super > # other stuff > end > > and the breakpoint never gets hit. > > --Jonathandef initialize(attributes = nil) super(attributes) # other stuff end perhaps. Also, how are you calling the constructor? Are you specifically doing MySubClass.new() ? -Tudor -- Posted via http://www.ruby-forum.com/.
Francois Beausoleil
2005-Nov-25 13:19 UTC
Re: overriding initialize or new for a subclass of ActiveRecord
Hi ! 2005/11/24, John Meredith <john@psynix.com>:> Make sure you call super ie.I saw a message, I think it was yesterday, that said you would be better off if you used after_initialize: class Model def after_initialize # do stuff end end Hope that helps ! -- François Beausoleil http://blog.teksol.info/ _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ezra Zygmuntowicz
2005-Nov-25 13:37 UTC
Re: Re: overriding initialize or new for a subclass of ActiveRec
Johnathan- The initialize mthod of your class that inherits from AR::Base does not get called in the request/response chain. So you should use a befopre_filter instead like this: class MyModel < ActiveRecord::Base before_filter :do_stuff def do_stuff ... end end HTH -Ezra On Nov 24, 2005, at 8:29 PM, Jonathan Leonard wrote:> Thanks for the answer, but I tried that as well. I put a breakpoint in > the func like so: > > def initialize > breakpoint > super > # other stuff > end > > and the breakpoint never gets hit. > > --Jonathan > > > > john wrote: >> Hi Jonathan, >> >> Make sure you call super ie. >> >> def initialize >> super >> >> # ... do stuff ... >> end >> >> - John >> >> On 25/11/2005, at 2:11 PM, Jonathan Leonard wrote: >> >>> >>> -- >>> Posted via http://www.ruby-forum.com/. >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> -- >> John Meredith <jmeredith-0iPedL6B8ETQT0dZR+AlfA@public.gmane.org> > > > -- > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732
Jonathan Leonard
2005-Nov-26 04:06 UTC
Re: Re: overriding initialize or new for a subclass of Activ
Ahh. Thanks, I''ll try that and post back if it doesn''t work. --Jonathan ezra wrote:> Johnathan- > > The initialize mthod of your class that inherits from AR::Base does > not get called in the request/response chain. So you should use a > befopre_filter instead like this: > > class MyModel < ActiveRecord::Base > > before_filter :do_stuff > > > def do_stuff > ... > end > > end > > HTH > > -Ezra >-- Posted via http://www.ruby-forum.com/.
Jonathan Leonard
2005-Nov-26 10:43 UTC
Re: Re: overriding initialize or new for a subclass of Activ
ezra wrote:> Johnathan- > > The initialize mthod of your class that inherits from AR::Base does > not get called in the request/response chain. So you should use a > befopre_filter instead like this: > > class MyModel < ActiveRecord::Base > > before_filter :do_stuff > > > def do_stuff > ... > end > > end > > HTH > > -EzraThis is not working. It says ''undefined method: before_filter.'' I noticed that this is used in the controllers (for the login functionality). Could it be for controllers only? --Jonathan -- Posted via http://www.ruby-forum.com/.
Jonathan Leonard
2005-Nov-26 11:44 UTC
Re: overriding initialize or new for a subclass of ActiveRec
francois.beausoleil wrote:> Hi ! > 2005/11/24, John Meredith <john-0iPedL6B8ETQT0dZR+AlfA@public.gmane.org>: >> Make sure you call super ie. > > I saw a message, I think it was yesterday, that said you would be > better off if you used after_initialize: > > class Model > def after_initialize > # do stuff > end > end > > Hope that helps !That works. Thanks! -- Posted via http://www.ruby-forum.com/.