# I don't understand the way Rails know the good table for my model class

**URL:** <https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969>\
**Category:** Rails\
**Created:** [August 15, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969 "2008-08-15T16:45:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 15, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969/1 "2008-08-15T16:45:36Z")

</div>

Hi,

I have the following migration file :

class Personne \< ActiveRecord::Migration

def self.up

create\_table( :personnes ) {

|p|

p.string :prenom

p.string :nom

p.string :telephone

p.text :adresse

}

end

def self.down

drop\_table :personnes

end

end

* * *

My model is very simple :

class Personne \< ActiveRecord::Base

end

* * *

In my controller, I put the following code :

class CarnetController \< ApplicationController

def voir

# Find all the objet Personne from the table personnes

@personnes = Personne.find(:all)

end

end

* * *

And it works !!!, but I don’t understand how Rails can know that the  
class  
Personne should be mapped to the personnes table. Where is it stored ?  
It  
should be due to the migrate Personne class ?

Tx a lot for your help !

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 15, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969/2 "2008-08-15T16:47:58Z")

</div>

On 15 Aug 2008, at 15:40, removed\_email\_address@domain.invalid wrote:

> And it works !!!, but I don’t understand how Rails can know that the  
> class  
> Personne should be mapped to the personnes table. Where is it stored ?  
> It  
> should be due to the migrate Personne class ?

It’s not stored anywhere. The rails convention is that the table name  
should be the plural lowercased, underscored form of the class name.  
Rails knows your class is called Personne, so it will assume the table  
is personnes.

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 15, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969/3 "2008-08-15T16:53:50Z")

</div>

But, this is dangerous because in French, the plural form is not  
constant ?

For sample a horse is called “cheval”, the plural form is “chevaux”  
not “chevals”. I doubt  
Rails support a dictionnary for knowing the good plural form. Is there  
way to change this  
behavior ?

Tx a lot !

On Aug 15, 4:47 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 15, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969/4 "2008-08-15T17:06:42Z")

</div>

On 15 Aug 2008, at 15:52, removed\_email\_address@domain.invalid wrote:

> But, this is dangerous because in French, the plural form is not  
> constant ?

It’s not constant in english either.

> For sample a horse is called “cheval”, the plural form is “chevaux”  
> not “chevals”. I doubt  
> Rails support a dictionnary for knowing the good plural form. Is there  
> way to change this  
> behavior ?

Rails comes with a bunch of rules for how it should pluralize things  
(see active\_support/inflections.rb;  
[http://github.com/rails/rails/tree/master/activesupport/lib/active\_support/inflections.rb](http://github.com/rails/rails/tree/master/activesupport/lib/active_support/inflections.rb))  
. These rules work for english, if you’re doing everything in  
another language you’ll have to add your own inflection rules

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 15, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969/5 "2008-08-15T17:36:43Z")

</div>

> Thank you, but can I override it ?, I don’t want to change the Rails  
> code :  
> Yes you can override this default behavior in your model class using:

set\_table\_name “my\_table”

Example from ActiveRecord::Base documentation:

class Mouse \< ActiveRecord::Base  
set\_table\_name “mice”  
…  
…  
end

Alexandre Brillant wrote:

> Thank you, but can I override it ?, I don’t want to change the Rails  
> code :
> 
> I try inside my controller to override it, but I can’t, the problem is  
> that  
> the activesupport cannot  
> be found ?
> 
> _require ‘activesupport/inflections’_
> 
> class CarnetController \< ApplicationController  
> layout ‘carnet1’
> 
> ```
> * ActiveSupport::Inflector.inflections do |inflect|
> inflect.plural( /$/, 's' )
> inflect.plural( /s$/, 's' )
> inflect.plural( /al$/, 'aux' )
> end*
> 
> def voir
> @personnes = Personne.find(:all)
> end
> 
> ```
> 
> end
> 
> On Fri, Aug 15, 2008 at 5:05 PM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 15, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969/6 "2008-08-15T17:42:27Z")

</div>

On 15 Aug 2008, at 16:32, Alexandre Brillant wrote:

> Thank you, but can I override it ?, I don’t want to change the Rails  
> code :

> I try inside my controller to override it, but I can’t, the problem  
> is that the activesupport cannot  
> be found ?

That’s not the right place to do it. Depending on your version of  
rails there’s either an example at the bottom of environment.rb or in  
config/initializers.  
You’ll probably want to clear out existing rules, which you can do  
with clear

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 15, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969/7 "2008-08-15T17:33:04Z")

</div>

Thank you, but can I override it ?, I don’t want to change the Rails  
code :

I try inside my controller to override it, but I can’t, the problem is  
that  
the activesupport cannot  
be found ?

_require ‘activesupport/inflections’_

class CarnetController \< ApplicationController  
layout ‘carnet1’

```
  * ActiveSupport::Inflector.inflections do |inflect|
      inflect.plural( /$/, 's' )
      inflect.plural( /s$/, 's' )
      inflect.plural( /al$/, 'aux' )
    end*

def voir
    @personnes = Personne.find(:all)
end

```

end

On Fri, Aug 15, 2008 at 5:05 PM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 15, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/i-dont-understand-the-way-rails-know-the-good-table-for-my-model-class/145969/8 "2008-08-15T17:50:04Z")

</div>

Thank you!, exactly what I need.

On Fri, Aug 15, 2008 at 5:36 PM, Robert W. \<
