Ruby on Rails plural (controller) and singular (model) convention - explanation

Ruby on-RailsRubyActiverecord

Ruby on-Rails Problem Overview


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)?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

Because the table holds users. Its just the convention.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1322512View Question on Stackoverflow
Solution 1 - Ruby on-RailsAMIC MINGView Answer on Stackoverflow
Solution 2 - Ruby on-RailsEmilyView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJustin D.View Answer on Stackoverflow
Solution 4 - Ruby on-RailsGanesh ArulananthamView Answer on Stackoverflow
Solution 5 - Ruby on-RailsJohn Paul AshenfelterView Answer on Stackoverflow