ChrisWesAllen
ChrisWesAllen

Reputation: 4975

Ruby/Rails - Models Named with Two Words (Naming Convention Issues)

This is really a question about naming conventions.

I have a model called PromotedEvents

The file is called promoted_events.rb

I created the table with:

create_table :promoted_events do |t|

Now I'm having problems creating anything, so I'm wondering if theres some problem using model with two words

im in the console and tried

a = PromotedEvents.new

a = Promoted_Event.new

a = promoted_event.new

and keep getting a nameerror : uninitialized constant error

Any ideas?

Upvotes: 36

Views: 35078

Answers (4)

Danny
Danny

Reputation: 4114

If you are an extreme rails n00b like me, then you will want to remember to create a class definition for your newly created table and place it in app/models.

It would go like

class LargeCat < ActiveRecord::Base
    belongs_to :zoo
end

Upvotes: 1

markquezada
markquezada

Reputation: 8515

If it helps, I always think of it like this:

The model name is singular because it represents a single, specific thing. So, PromotedEvent is a specific promoted event that has a name, date, etc.

The table name on the other hand is plural. This is because the table stores a collection of these singular items. So, promoted_events.

In rails, filenames are mostly a matter of convention since ruby has pretty lax rules in this regard, but generally it's class_name.rb. This page might help you get a better overview of what conventions are used where and what is specific to Ruby versus Rails.

Upvotes: 10

loosecannon
loosecannon

Reputation: 7803

Model names are singular and camel case like so pe = PromotedEvent.new()

the file should be promoted_event.rb

Controllers are plural

PromotedEventsController

constants are ALL_CAPS

locals are separated_by_underscores_and_lowercase

table names are plural 'SELECT * FROM promoted_events`

Upvotes: 22

Alex Wayne
Alex Wayne

Reputation: 186984

Your class should be singlular.

Name it PromotedEvent in the file promoted_event.rb

a = PromotedEvent.new

Upvotes: 54

Related Questions