Ruby on Rails creating constant on model

11.35 Muhamad Akbar Bin Widayat 0 Comments

Ruby on Rails
Ruby on Rails (Photo credit: Wikipedia)
Follow me to create a constant on model. Many people ask where a good place to take a constant. I recommend to you to take a constant on your model.

For instance:
1. I have song.rb

class Song < ActiveRecord::Base
  #--
  # constants
  #++
  KEY_SIMBOLS = [ "C", "C#", "D", "D#", "E", "F", "F#", "G",
     "G#", "A", "A#", "B" ]

  #--
  # public class methods
  #++
 def initial_key
   initial_key_sym = KEY_SIMBOLS
 end
end
2. I have constant KEY_SIMBOLS
3. You can access with:
song = Song.first
song.initial_key

Thanks
Enhanced by Zemanta

0 komentar:

terimakasih komentarnya