Forum: Ruby on Rails globally affecting pluralization AND primary_key for all mod

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ara.t.howard (Guest)
on 2005-12-07 19:32
(Received via mailing list)
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
===============================================================================
technoweenie (Guest)
on 2005-12-07 19:37
(Received via mailing list)
On 12/7/05, Ara.T.Howard <removed_email_address@domain.invalid> wrote:
> and didn't want ot have to edit every generated model so i used:
> in environment.rb
>
> regards.
>
> -a


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 :)

--
rick
http://techno-weenie.net
ara.t.howard (Guest)
on 2005-12-07 19:49
(Received via mailing list)
On Wed, 7 Dec 2005, Rick O. 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
===============================================================================
This topic is locked and can not be replied to.