About RAILS

Hi,

I don’t understand one point with Ruby on Rails :

I have the following migration file :
class Personne < ActiveRecord::Migration

def self.up

Commentaire à enlever si vous avez déjà une table personnes

drop_table :personnes

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 classe
Personne should be mapped to the

personnes table. Where it is stored ?

Txa lot !

Alexandre Brillant wrote:

I don’t understand one point with Ruby on Rails :

The best place to discuss RoR is here:

http://groups.google.com/group/rubyonrails-talk/

This forum is only qualified to discuss Ruby, the language behind Rails,
and
many other awesome programs.

create_table( :personnes ) {

|p|

I don’t know what editor you use, but ‘script/generate model’ should
have
created better syntax there. The |p| should be up near the {, as a style
thing.
And your editor might have inserted linefeeds when you tried to paste
your
source here.

I would scratch this project and use ‘script/generate model’ to redo the
files.

And it works, but I don’t understand how Rails can know that the classe
Personne should be mapped to the

personnes table. Where it is stored ?

Google for “convention over configuration”. Then pity the programmers,
using
other platforms, who must edit ~1 Kb of lines of configuration files,
just to
stitch things together!

By default, ActiveRecord will read the name of your class, Personne,
lower-case
it, personne, and pluralize it: personnes. Then it looks for that table.

It uses a class called Inflector to do this.

However, I suspect you are lucky to use a language that pluralizes with
an s. I
don’t think the Inflector is smart enough to divine your language and
pluralize
with its conventions - if any! Maybe someone on the RoR forum knows how
to set
your locale to French, to handle French idioms correctly…

Tx philip, I will go to the google forum.

Well for the |p| after this is correct from the syntax point of view,

create_table( … ) { |p|

or

create_table( … ) {
|p|

are both ok, I prefer the second one

“Phlip” [email protected] a écrit dans le message de news:
[email protected]

Thank you for the generate model command, I will try it to see the
result.

Thank you too for the remark about the multi-lines do end idiom.

:slight_smile:

“Phlip” [email protected] a écrit dans le message de news:
[email protected]

Alexandre Brillant wrote:

are both ok, I prefer the second one

I prefer the output of script/generate model, because it gets so much
ready for
you all at once. For example, it also creates matching test suites and
fixtures.

Also, you will learn that all languages come with established styles.
For
example, “everyone” typically writes a one-liner block with {}, but they
write a
multi-line block with do-end.

Knowing why and how people use different Ruby idioms will help you match
your
style to your team’s style, if any. And it will help you break that
style when
you know why you are breaking it.