Ara.T.Howard
2005-Dec-07  17:28 UTC
globally affecting pluralization AND primary_key for all models
i know ActiveRecord::Base has a hook to affect pluralization on/off...  but i
couldn''t find one for affecting the primary key?  i have a database
with about
30 tables of the form
   create table foo (
     foo_id serial,
     ...
   );
and didn''t want ot have to edit every generated model so i used:
   class ActiveRecord::Base
     def self::inherited other
       ret = super
       other.table_name = other.table_name.singularize
       other.primary_key = "#{ other.table_name }_#{ other.primary_key
}"
       ret
     end
   end
in environment.rb
and thus
   ./script/console
   >> Foo.table_name
   => "foo"
   >> Foo.primary_key
   => "foo_id"
works for all tables.  is there a cleaner way to do this?
regards.
-a
-- 
==============================================================================|
ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy.  all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================
Rick Olson
2005-Dec-07  17:33 UTC
Re: globally affecting pluralization AND primary_key for all models
On 12/7/05, Ara.T.Howard <ara.t.howard-32lpuo7BZBA@public.gmane.org> wrote:> > i know ActiveRecord::Base has a hook to affect pluralization on/off... but i > couldn''t find one for affecting the primary key? i have a database with about > 30 tables of the form > > create table foo ( > foo_id serial, > ... > ); > > and didn''t want ot have to edit every generated model so i used: > > class ActiveRecord::Base > def self::inherited other > ret = super > other.table_name = other.table_name.singularize > other.primary_key = "#{ other.table_name }_#{ other.primary_key }" > ret > end > end > > in environment.rb > > and thus > > ./script/console > >> Foo.table_name > => "foo" > >> Foo.primary_key > => "foo_id" > > works for all tables. is there a cleaner way to do this? > > regards. > > -aActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore Don''t ask me where to find that either, I found that little nugget in the rails book :) -- rick http://techno-weenie.net
Ara.T.Howard
2005-Dec-07  17:46 UTC
Re: globally affecting pluralization AND primary_key for all models
On Wed, 7 Dec 2005, Rick Olson wrote:> ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore > > Don''t ask me where to find that either, I found that little nugget in the > rails book :)cool. saw that here >> ActiveRecord::Base.methods.grep /key/ => ["primary_key_prefix_type", "primary_key_prefix_type=", "set_primary_key_prefix_type", "reset_primary_key", "primary_key", "primary_key=", "set_primary_key"] but didn''t know what the args were and the doc site was down attm... thanks! -a -- ==============================================================================| ara [dot] t [dot] howard [at] noaa [dot] gov | all happiness comes from the desire for others to be happy. all misery | comes from the desire for oneself to be happy. | -- bodhicaryavatara ===============================================================================