RoR Naming Conventions?

Brand new one here :slight_smile:

I’ve looked and googled and searched.

There are lots of references to RoR Naming Conventions - I haven’t
actually been able to locate the list of these conventions.

Is there a single list of conventions?
Or are they defined through out the documentation and you pick them up
as you go?

I’m looking for the Database ones right now.

I’ve got 8 text files of exported data that I’m putting into a
database and building my first RoR app around this data.

How should this table be done in relation to the RoR conventions?

CREATE TABLE makes (
code varchar(4) NOT NULL,
Description varchar(30) NOT NULL,
PRIMARY KEY (code)
);

CREATE TABLE years (
make_code varchar(4) NOT NULL,
family varchar(7) NOT NULL,
vehicleType varchar(2) NOT NULL,
yearGroup int(4) NOT NULL,
monthGroup int(2) NOT NULL,
description varchar(10) NOT NULL,
KEY make_code (make_code),
KEY family (family),
KEY vehicleType (vehicleType),
KEY yearGroup (yearGroup),
KEY monthGroup (monthGroup)
);

Thanks :slight_smile:

On Oct 25, 2007, at 9:54 PM, Nicholas O. wrote:

PRIMARY KEY (code)
);

Use generic ‘id’ values as your primary keys.
Keep your column names in typical_ruby_variable_style and certainly
not with a capital letter to start since that indicates a constant to
Ruby.

KEY vehicleType (vehicleType),
KEY yearGroup (yearGroup),
KEY monthGroup (monthGroup)
);

Thanks :slight_smile:

Your migration for these tables will have something like:

create_table :makes do |t|
t.column :code, :string, :limit => 4, :null => false
t.column :description, :string, :limit => 30, :null => false
end
add_index :makes, :code

create_table :years do |t|
t.column :make_code, :string, :limit => 4, :null => false
t.column :family, :string, :limit => 7, :null => false
t.column :vehicle_type, :string, :limit => 2, :null => false
t.column :year_group, :integer, :null => false
t.column :month_group, :integer, :null => false
t.column :description, :string, :limit => 10, :null => false
end
add_index :years, :make_code
add_index :years, :family
add_index :years, :vehicle_type
add_index :years, :year_group
add_index :years, :month_group

You should then also have validations in your models for the size
limits, for example:
class Make
validates_length_of :code, :in => 1…4
validates_length_of :description, :in => 1…30
end

See more at:
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/
SchemaStatements.html#M000716

You probably also want to avoid names that are “magic” for
ActiveRecord like ‘type’, ‘id’, ‘position’, ‘updated_at’. I know
that you can find a list of these to avoid.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Is there a single list of conventions?
Or are they defined through out the documentation and you pick them up
as you go?

I don’t think there’s an official list, it would in the Rails wiki or
dev.rubyonrails.org, something like that, but i did find these

http://wiki.rubyonrails.org/rails/pages/TipSheetForBeginners
http://areyoubeewhy.com/2007/10/18/ruby-101-naming-conventions

I don’t think there’s an official list, it would in the Rails wiki or
dev.rubyonrails.org, something like that, but i did find these

http://wiki.rubyonrails.org/rails/pages/TipSheetForBeginners
http://areyoubeewhy.com/2007/10/18/ruby-101-naming-conventions

Ruby Programming

That’s great,

Thanks for that too Gene :wink:

Can’t remember where I got this but here we go:

Variables/Methods: snake_case
Class names: CamelCase
Constants: ALL CAPITALS

Please someone advice if I’m wrong :slight_smile:

Elle

On 10/26/07, Rob B. [email protected] wrote:

Is there a single list of conventions?
CREATE TABLE makes (

KEY yearGroup (yearGroup),
end
add_index :years, :make_code
end

Rob B. http://agileconsultingllc.com
[email protected]

Thanks a lot for that Rob.

I didn’t make up this data structure. I know it totally doesn’t fit the
RoR
way, hence why I’m looking for the RoR way so I can adapt it :slight_smile:

I’ve also looked some more and found this list which is helpful :slight_smile:
http://blog.invisible.ch/2006/05/01/ruby-on-rails-reference/

Nick

On 10/25/07, Nicholas O. [email protected] wrote:

as you go?
Description varchar(30) NOT NULL,
PRIMARY KEY (code)
);

Unfortunately, since you say later that this is a legacy schema that
you aren’t able to change, you are facing more than just naming
conventions.

ActiveRecord doesn’t have any built-in support for non-integer primary
keys. You might google ‘ActiveRecord noninteger keys’ to look at what
others have done in this case.

KEY yearGroup (yearGroup),
KEY monthGroup (monthGroup)
);

In this table, it’s not clear (to me anyway) what the primary key is.
I think that in MySQL the default primary key would be make_code since
it’s the first indexed non-null field.

On the other hand, if this table really has a composite primary key,
then again, AR doesn’t directly support that, although there is some
outside work on supporting CPKs in AR:

http://compositekeys.rubyforge.org/


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Oct 25, 9:40 pm, “Nicholas O.” [email protected] wrote:

Thanks for that too Gene :wink:
I finally found it,
Peak Obsession

I think for each model/column/controller/action/view, there’s a couple
dozen names you shouldn’t use. Here’s some things that can go wrong:
if you name a controller “files”:
http://www.ruby-forum.com/topic/103619

partial named “flas”

model named “type”
http://www.movesonrails.com/articles/2007/02/23/tip-of-the-day

column named “type”
http://www.benlog.org/2007/1/16/legacy-rails-beware-of-type-columns

Attached is a little helper class I used on a project. May come in
handy,
may not.


Andrew S.

On 10/27/07, Rick DeNatale [email protected] wrote:

Unfortunately, since you say later that this is a legacy schema that
you aren’t able to change, you are facing more than just naming
conventions.

ActiveRecord doesn’t have any built-in support for non-integer primary
keys. You might google ‘ActiveRecord noninteger keys’ to look at what
others have done in this case.

I’m making a brand new app - I can do whatever the hell I want :slight_smile:
I’m going to have to have an import function, to map old to new which is
fine by me.

Nick

On 10/28/07, Nicholas O. [email protected] wrote:

I’m making a brand new app - I can do whatever the hell I want :slight_smile:
I’m going to have to have an import function, to map old to new which is
fine by me.

Good, glad you’ve got the freedom. Sometimes, I’m not quite so lucky.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/