Posts Tagged active

Overriding Active Record

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"

Add comment April 29, 2008


Categories

posts[:recent]

episodes[:recycled]

@@name = PRAYAS

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

find_by_tags

Links

visitors[:since_Mar'08]

free web counter

Spam Blocked

Feeds

Meta

RSS Prayas here