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

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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


John M. [email protected]

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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


John M. [email protected]

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

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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732

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

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

francois.beausoleil wrote:

Hi !
2005/11/24, John M. [email protected]:

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!

Hi !
2005/11/24, John M. [email protected]:

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 !