Forum: Ruby on Rails Table prefix

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.
1477a35ec1688db1a92af32a0b6ad782?d=identicon&s=25 Davide Bortolini (Guest)
on 2006-01-11 20:28
(Received via mailing list)
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
E48d29dc8fedb2878fa518d41cc63d88?d=identicon&s=25 Jan Prill (Guest)
on 2006-01-11 21:14
(Received via mailing list)
Hi, Davide,

have a look at ActiveRecord::Base#set_table_name
(http://api.rubyonrails.com/classes/ActiveRecord/Ba...) and
http://wiki.rubyonrails.com/rails/pages/HowToUseLe...

Best regards
Jan Prill
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-01-12 01:11
(Received via mailing list)
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
This topic is locked and can not be replied to.