I’m trying to define a method that will be available to all Models. I
originally put it in the ApplicationHelper file, but I now understand
that this is only available to Views. Where do I put a common model
method then?
PS: If it makes a difference, the method needs to be able to reference
the calling model, i.e. passing the model instance as an argument
you can create a module mix you application_controller
type error, must mix you model
On 11��10��, ����2ʱ25��, Chris B. [email protected]
I’m sorry, I don’t understand. Do you mean that I can add it to my
Application controller? According to the comments in that file, anything
added will be available to all controllers, but it doesn’t mention
models. I need it to be available to models, including when run from the
console.
Or you could create a new base class that inherits from
ActiveRecord::Base and have your models inherit from your new base
class.
If I were to go this route and create a new base class, do i put it in
it’s own file in the model folder, and then do I require that file in
each related model, or in the application controller, or somewhere else?
Yes, you can put it in the model directory, mark it as an abstract
model class, and make sure it won’t collide with any other model.
You don’t have to explicitly call require in other models, just
specify it as the parent class, and Rails with automatically load the
specific file. Make sure to follow the Class name -> filename
convention, so Rails can find it. Have Rails autoload it, so it can
reload the file when needed in development mode.