# Models added functionality

**URL:** <https://www.ruby-forum.com/t/models-added-functionality/143127>\
**Category:** Rails\
**Created:** [July 15, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/models-added-functionality/143127 "2008-07-15T15:12:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 15, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/models-added-functionality/143127/1 "2008-07-15T15:12:29Z")

</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:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 15, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/models-added-functionality/143127/2 "2008-07-15T16:15:53Z")

</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=’’)  
return [code + (description.blank? ? ‘’ : ’ - ’ + description),  
code]  
end

Thanks.

Pepe

On Jul 15, 10:08 am, Matthew R. Jacobs \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 15, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/models-added-functionality/143127/3 "2008-07-15T16: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  
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.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 15, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/models-added-functionality/143127/4 "2008-07-15T16:47:31Z")

</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

On Jul 15, 10:21Â am, Matthew R. Jacobs \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 15, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/models-added-functionality/143127/5 "2008-07-15T16:21:30Z")

</div>

pepe wrote:

> 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
> 
> On Jul 15, 10:08ï¿½am, Matthew R. Jacobs \<rails-mailing-l…@andreas-

ok,  
so that’s something you just use in views…

so it should probably go in application\_helper.rb

# blah.erb.html

# \<%= select(:user, :countries, select\_options(@all\_countries)) %\>

# application\_helper.rb

def select\_options(records)  
records.map do |record|  
format\_select\_option(record.name, record.description)  
end  
end

# def format\_select\_option(code, description=’’) return [code + (description.blank? ? ‘’ : ’ - ’ + description), code] end

or something?
