Models added functionality

Hello.

I have a piece of code (method) that I want all my models to have available. What would be the best way to implement it?

Thanks

pepe wrote:

Hello.

I have a piece of code (method) that I want all my models to have available. What would be the best way to implement it?

Thanks

What sort of code is it? Best way may be to extend ActiveRecord::Base

take a look at some plugins for examples.

eg. ActsAsSolr http://github.com/thomas/acts_as_solr/tree/master/lib/acts_as_solr.rb

it defines a module;

module ActsAsSolr   module ActsMethods     ...     stuff to extend ActiveRecord::Base     ...   end end then does the extend.

# reopen ActiveRecord and include the acts_as_solr method ActiveRecord::Base.extend ActsAsSolr::ActsMethods

You'll see the same quite a bit with a;

module BlahBlahBlah::InstanceMethods   ... end

ActiveRecord::Base.include(BlahBlahBlah::InstanceMethods)

just play around in console, see what feels most comfortable.

It's just a piece of code to format data that will go in a form.select. Something like this:

def format_select_option(code, description='')   return [code + (description.blank? ? '' : ' - ' + description), code] end

Thanks.

Pepe

Yes, it is something destined for the views. I was a little torn because I didn't want to put anything in the views or the models that didn't belong there. I just didn't think about passing the result set to a method from the view. You just gave me what I actually was looking for from the beginning.

Thanks a lot!

Pepe