Forum: Ruby on Rails overriding initialize or new for a subclass of ActiveRecord

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fef8f651fefa7b23683a4e57041474d2?d=identicon&s=25 Jonathan Leonard (johanatan)
on 2005-11-25 05:11
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
F9ea8d60603f896798d85d8a41491066?d=identicon&s=25 john (Guest)
on 2005-11-25 05:16
(Received via mailing list)
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@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

--
John Meredith <jmeredith@psynix.com>
Fef8f651fefa7b23683a4e57041474d2?d=identicon&s=25 Jonathan Leonard (johanatan)
on 2005-11-25 05:29
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@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>
> --
> John Meredith <jmeredith@psynix.com>
18f328d012d3e2e2e4eedf3b30ee04be?d=identicon&s=25 Tudor Oprea (toprea)
on 2005-11-25 09:15
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.
>
> --Jonathan

 def initialize(attributes = nil)
   super(attributes)
   # other stuff
 end

perhaps. Also, how are you calling the constructor?  Are you
specifically doing

  MySubClass.new() ?

-Tudor
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 francois.beausoleil (Guest)
on 2005-11-25 14:21
(Received via mailing list)
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 !
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 ezra (Guest)
on 2005-11-25 14:41
(Received via mailing list)
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:

>
>>    super
>>> Posted via http://www.ruby-forum.com/.
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
Fef8f651fefa7b23683a4e57041474d2?d=identicon&s=25 Jonathan Leonard (johanatan)
on 2005-11-26 05:06
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
>
Fef8f651fefa7b23683a4e57041474d2?d=identicon&s=25 Jonathan Leonard (johanatan)
on 2005-11-26 11:43
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

This 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
Fef8f651fefa7b23683a4e57041474d2?d=identicon&s=25 Jonathan Leonard (johanatan)
on 2005-11-26 12:44
francois.beausoleil wrote:
> 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 !

That works.  Thanks!
This topic is locked and can not be replied to.