# Models added functionality

**URL:** <https://discuss.rubyonrails.org/t/models-added-functionality/31877>\
**Category:** rubyonrails-talk\
**Created:** [July 15, 2008, 1:11pm UTC](https://discuss.rubyonrails.org/t/models-added-functionality/31877 "2008-07-15T13:11:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [July 15, 2008, 1:11pm UTC](https://discuss.rubyonrails.org/t/models-added-functionality/31877/1 "2008-07-15T13:11:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 15, 2008, 2:08pm UTC](https://discuss.rubyonrails.org/t/models-added-functionality/31877/2 "2008-07-15T14:08:45Z")

</div>

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](http://github.com/thomas/acts_as_solr/tree/master/lib/acts_as_solr.rb)

it defines a module;

module ActsAsSolr &nbsp;&nbsp;module ActsMethods &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;stuff to extend ActiveRecord::Base &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;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 &nbsp;&nbsp;... end

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

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

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [July 15, 2008, 2:15pm UTC](https://discuss.rubyonrails.org/t/models-added-functionality/31877/3 "2008-07-15T14:15:11Z")

</div>

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='') &nbsp;&nbsp;return [code + (description.blank? ? '' : ' - ' + description), code] end

Thanks.

Pepe

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [July 15, 2008, 2:47pm UTC](https://discuss.rubyonrails.org/t/models-added-functionality/31877/4 "2008-07-15T14:47:13Z")

</div>

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
