Extending the ActiveRecord:Base class

Dear list,

I’d like to make an ‘after_find’ call for each and every AR model I
defined. In AWDWR page 376 they do so by opening up the AR class like
this:
class ActiveRecord::Base
def self.some_method
return ‘foo’
end
end

All nice and well but where should I store this class?? According to
the chapter it should go in app/models. So I placed a file called
base.rb in app/models/active_record. To no avail. Placing it in app/
models did no good either.

I know I can do this by defining a module in lib/ and include that in
all my models but that is just not as pretty.

With kind regards,
Harm

On 4 Dec 2007, at 13:40, harm wrote:

end

All nice and well but where should I store this class?? According to
the chapter it should go in app/models. So I placed a file called
base.rb in app/models/active_record. To no avail. Placing it in app/
models did no good either.

I know I can do this by defining a module in lib/ and include that in
all my models but that is just not as pretty.

Put it wherever you want. In environment.rb require that file.
An alternative would be

class MyBaseClass < ActiveRecord::Base
self.abstract_class = true
[your funkiness here]
end

and then have all your classes inherit from MyBaseClass

Fred

Aha! So I should just require it. I was hoping that would go
‘automagically’. Thanks for the reply.

On Dec 4, 3:02 pm, Frederick C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs