Globally affecting pluralization AND primary_key for all mod


#1

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


#2

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


rick
http://techno-weenie.net


#3

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

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