Overriding Active Record

April 29, 2008

1. If one wishes the table name should be singular instead of plural
Set the below configuration

ActiveRecord::Base.pluralize_table_names = false

2. If you want your model’s classname should represent a table of someother name

class Name < ActiveRecord::Base
   # by defualt it refers to "names" table in DB
   # Now let it refer to "nicknames"
   set_table_name "nickname"
end

3. If your primary key column is not named simply “id”, you can override this from within the class definition as well:

class Name < ActiveRecord::Base
     set_primary_key "nameid"
end

so when you want to save a record in DB (you must still use the id attribute to do so)

name_obj = Name.new
name_obj.id = 'N999'   #use the same   format this is eqivaled in saying name_obj.nameid='N999'
name_obj.save

but while accessing such record

print name_obj.nameid  # gives a result of "N999"

Entry Filed under: Rails. Tags: , , , , , , , .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


@@name=PRAYAS

gap_resize.jpg

Time.now()

April 2008
M T W T F S S
« Mar   May »
 123456
78910111213
14151617181920
21222324252627
282930  

order[:category]

posts[:recent]

episodes[:recycled]

links[:favourite]

vote_for_web2.0

Step down at my blog with your ideas,comments,suggestions on Ruby,RoR,Ajax or Web2.0 Technology. You may reach me at rajesh4it@gmail.com.

@visitors.count(Feb’08)

find_all(:categories)

Around me bollywood coding news@RoR Rails RoR Ruby Tools&Tips Web2.0 talks

Vote for RoR

Blog Stats