Forum: Ruby on Rails Table with Constants

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
91a469d9f43a066babfd5531311d0fd1?d=identicon&s=25 Rath (Guest)
on 2006-02-17 05:11
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 = ?',

Can anyone help me?
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-02-17 09:31
(Received via mailing list)
I have a plugin which may be what you're looking for.

The announcement (with a brief intro) is here:

and the subversion URL is:


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

>  ....
>   def find_by_type (type)
> Rails mailing list

Trevor Squires
C77b15d1fd19b42984c35cfe3260a1d5?d=identicon&s=25 Sam Pohlenz (Guest)
on 2006-02-17 09:37
(Received via mailing list)
Rath wrote:
> Can anyone help me?

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

 def self.find_by_type(type)
   find(:all, ....)

27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-02-17 10:23
(Received via mailing list)

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

The 'self' makes it a class method so you can call

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

91a469d9f43a066babfd5531311d0fd1?d=identicon&s=25 Rath (Guest)
on 2006-02-17 11:03
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) :)


This topic is locked and can not be replied to.