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? -- Posted via http://www.ruby-forum.com/.
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- rails-plugin and the subversion URL is: http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk Regards, Trevor On 16-Feb-06, at 8:11 PM, Rath wrote:> 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Trevor Squires http://somethinglearned.com
Rath wrote:> > 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? >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
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 On 17-Feb-06, at 12:30 AM, Trevor Squires wrote:> 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-rails-plugin > > and the subversion URL is: > > http://svn.protocool.com/rails/plugins/enumerations_mixin/trunk > > Regards, > Trevor > > > On 16-Feb-06, at 8:11 PM, Rath wrote: > >> 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? >> >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > Trevor Squires > http://somethinglearned.com > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
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 -- Posted via http://www.ruby-forum.com/.