# Table prefix

**URL:** <https://www.ruby-forum.com/t/table-prefix/102325>\
**Category:** Rails\
**Created:** [January 11, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/table-prefix/102325 "2006-01-11T20:28:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nutsmuggler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/57b2e6/32.png) [@nutsmuggler](https://www.ruby-forum.com/u/nutsmuggler)\
**Post date:** [January 11, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/table-prefix/102325/1 "2006-01-11T20:28:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nutsmuggler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/57b2e6/32.png) [@nutsmuggler](https://www.ruby-forum.com/u/nutsmuggler)\
**Post date:** [January 11, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/table-prefix/102325/2 "2006-01-11T21:14:32Z")

</div>

Hi, Davide,

have a look at ActiveRecord::Base#set\_table\_name  
([Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000740)) and  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowToUseLegacySchemas)

Best regards  
Jan P.

---

<div class="post-metadata">

**Author:** ![nutsmuggler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/57b2e6/32.png) [@nutsmuggler](https://www.ruby-forum.com/u/nutsmuggler)\
**Post date:** [January 12, 2006, 1:11am UTC](https://www.ruby-forum.com/t/table-prefix/102325/3 "2006-01-12T01:11:57Z")

</div>

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
