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

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

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

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 !

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)   . These rules work for english, if you're doing everything in
another language you'll have to add your own inflection rules

Fred

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

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 ?

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

Thank you!, exactly what I need.