# Table with Constants

**URL:** <https://www.ruby-forum.com/t/table-with-constants/53930>\
**Category:** Rails\
**Created:** [February 17, 2006, 5:11am UTC](https://www.ruby-forum.com/t/table-with-constants/53930 "2006-02-17T05:11:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [February 17, 2006, 5:11am UTC](https://www.ruby-forum.com/t/table-with-constants/53930/1 "2006-02-17T05:11:48Z")

</div>

I would like to have a table with some constants to use them later on  
for a drop down menu. How can this be done?

My table looks like

## Table with constants

## id | key | value | type

1 | M | Male | sex  
…

I know it can be done like this  
\<% select ‘object’, ‘method’, Constants.find(:all, :select =\> [“key,  
value”], :conditions =\> [‘type = ?’, “sex”]) %\>

I prefer to have something in the ConstantsModel to return an array like

class Constants \< ActiveRecord::Base  
def find\_by\_type (type)  
find(:all, :select =\> [“key, value”], :conditions =\> [‘type = ?’,  
type])  
end  
end

Can anyone help me?

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [February 17, 2006, 9:31am UTC](https://www.ruby-forum.com/t/table-with-constants/53930/2 "2006-02-17T09:31:47Z")

</div>

I have a plugin which may be what you’re looking for.

The announcement (with a brief intro) is here:

[http://www.somethinglearned.com/articles/2005/10/25/announcement-new-](http://www.somethinglearned.com/articles/2005/10/25/announcement-new-)  
rails-plugin

and the subversion URL is:

[http://svn.protocool.com/rails/plugins/enumerations\_mixin/trunk](http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk)

Regards,  
Trevor

On 16-Feb-06, at 8:11 PM, Rath wrote:

> …  
> def find\_by\_type (type)  
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [February 17, 2006, 9:37am UTC](https://www.ruby-forum.com/t/table-with-constants/53930/3 "2006-02-17T09:37:49Z")

</div>

Rath wrote:

> Can anyone help me?

Prefix the method name with ‘self.’ to make it a class method like find.

i.e.  
def self.find\_by\_type(type)  
find(:all, …)  
end

-Sam

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [February 17, 2006, 10:23am UTC](https://www.ruby-forum.com/t/table-with-constants/53930/4 "2006-02-17T10:23:07Z")

</div>

Ugh,

I put it down to the fact that it’s really late and I’m tired and  
probably need food.

I thought you were asking for a nice way to handle (what you call)  
constants and it looks more like you just wanted to know how to  
create a class method.

def self.find\_by\_type  
#body of your method  
end

The ‘self’ makes it a class method so you can call  
ConstantsModel.find\_by\_type()

Sorry for the confusion, I’m going to bed.

Trevor

---

<div class="post-metadata">

**Author:** ![rath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rath](https://www.ruby-forum.com/u/rath)\
**Post date:** [February 17, 2006, 11:03am UTC](https://www.ruby-forum.com/t/table-with-constants/53930/5 "2006-02-17T11:03:48Z")

</div>

Thanks guys… that’s what I needed but I’m still not happy, there must  
be something easier than that… perhaps something like  
ConstantsModel.find\_by\_? but I will have to create a method for each  
“type” of constants… (perhaps it was not the best to call them  
“constants” because they are not) 🙂

cheers,

Rath
