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

Comments

Popular posts from this blog

Pengembang Perangkat Lunak Membangun Karya untuk UMKM

Kesalahan Pindah Blogspot ke Domain Pribadi Berujung Domain Blogspot Lenyap

Episode 5 - Meramu Nilai - Nilai Inti pada Produk