Hello, For a number of my models I have many constant key value pairs (sometimes dozens). I have considered putting them in the model but it ends up creating a very cluttered model file. Is there an approvaed approach to having an external data file that is associated with a model? Should I use a yaml file of the same name? xml? csv? Recommendations are appreciated. Thanks, Ryan Glover http://www.prioninteractive.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-Jan-14 01:15 UTC
Re: Storage of constants
Hi -- On Sun, 14 Jan 2007, Ryan Glover wrote:> > Hello, > > For a number of my models I have many constant key value pairs > (sometimes dozens). I have considered putting them in the model but it > ends up creating a very cluttered model file. Is there an approvaed > approach to having an external data file that is associated with a > model? Should I use a yaml file of the same name? xml? csv? > Recommendations are appreciated.How about Ruby? :-) Just define some constants and pull them in at runtime. You can put things in lib/ and then require them from your model file(s), or from the bottom of environment.rb. For example, you could create a file called extra_data.rb: module ExtraData COLORS = %w{ red orange yellow } end Then in your model file: require ''extra_data'' # now you have access to ExtraData::COLORS. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Glover wrote:> Hello, > > For a number of my models I have many constant key value pairs > (sometimes dozens). I have considered putting them in the model but it > ends up creating a very cluttered model file. Is there an approvaed > approach to having an external data file that is associated with a > model? Should I use a yaml file of the same name? xml? csv? > Recommendations are appreciated. > > Thanks, > Ryan Glover > http://www.prioninteractive.comYou could have a subfolder of app/models called constants. #app/models/foo.rb require ''constants/foo'' class Foo < AR::B include FooConstants self.bar = BAZ end #app/models/constants/foo.rb module FooConstants BAZ = "Foobar!" end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---