Overriding initialize or new for a subclass of ActiveRecord


#1

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


#2

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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


John M. removed_email_address@domain.invalid


#3

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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


John M. removed_email_address@domain.invalid


#4

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


#5

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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#6

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


#7

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


#8

francois.beausoleil wrote:

Hi !
2005/11/24, John M. removed_email_address@domain.invalid:

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!


#9

Hi !
2005/11/24, John M. removed_email_address@domain.invalid:

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 !