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 !
On 15 Aug 2008, at 15:40, [email protected] 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
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. [email protected]
On 15 Aug 2008, at 15:52, [email protected] 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)
. 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 :
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. <
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
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. <
Thank you!, exactly what I need.
On Fri, Aug 15, 2008 at 5:36 PM, Robert W. <