63

As per Ruby on Rails convention, controller names get pluralized while model names are singular. Example : a Users controller, but a User model.

rails generate controller Users
rails generate model User name:string email:string

Now open migration file

 class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email    
      t.timestamps
    end
  end
end

Here table name is plural (users).

So my question is - Why table name is plural (users) even though the model name is singular (User)?

infused
  • 24,000
  • 13
  • 68
  • 78

5 Answers5

58

Ruby on Rails follow linguistic convention. That means a model represents a single user, whereas a database table consists of many users.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
  • 3
    So what's the story for non-standard pluralizations? For instance, if my model were "Activity", would the table end up being "Activities" or "Activitys"? – sak Sep 20 '12 at 14:59
  • 6
    It would be "Activities" the rails helper method "pluralize" is very clever. It knows that the plural of 'person' is 'people', and so on. http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-pluralize – Matt Connolly Sep 27 '12 at 00:39
  • 2
    Does the Rails model pluralization convention assume models are written in English? – duhaime Jan 30 '16 at 19:23
13

An instance of your User model represents a single user, so is singular. The users table, by contrast, holds all of your users, so it's plural.

Emily
  • 17,813
  • 3
  • 43
  • 47
4

To complete Emily's answer

An instance of your User model represents a single user, so is singular. The users table, by contrast, holds all of your users, so it's plural.

Rimian
  • 36,864
  • 16
  • 117
  • 117
Justin D.
  • 4,946
  • 5
  • 36
  • 69
1

in rails conntroller and table name are plural model alone is singular.In a two word name second word is pluralized !

0

Because the table holds users. Its just the convention.

John Paul Ashenfelter
  • 3,135
  • 1
  • 22
  • 29