Ruby on Rails creating constant on model

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

Komentar

Postingan populer dari blog ini

EPrT English Proficiency Test Soal - soal

cara memunculkan dan menyembunyikan icon date, time, clock, volume, network, power battery, action center pada taskbar pada Windows 7