Table prefix

Hi everybody,
is it possible to override standard RoR table nomination/pluralization
to include a suffix? I have some tables with usr_ prefix but i would use
“real” table name in model naming. For example i have table
usr_accesslogs; in normal use i should create a model called
UsrAccesslog instead Accesslog.
Is it possible to do this??

Thanx

Davide

Hi, Davide,

have a look at ActiveRecord::Base#set_table_name
(Peak Obsession) and
Peak Obsession

Best regards
Jan P.

Another way to get prefixes for all your class is to do something like
the following.
Make a new folder in plugins called prefixed (or whatever)
Add a file init.rb with require ‘prefixed’ in it…
Add a directory ‘lib’ with a sinlge file ‘prefixed.rb’ in it.
Then in prefixed.rb add the following:

module ActiveRecord
class Base
class << Base
alias :original_table_name :table_name
def table_name
#could setup a lot more complicated rules here.
“my_prefix_#{original_table_name}”
end
end
end
end

that way everything will work as it used to, but also add the prefix
to all your models for free. I’m not sure what you could do with the
rules to get the prefix value, but something more interesting then
what I have there should be doable.

-Nick