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

一笑奈何 提交于 2019-12-17 17:25:41

问题


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


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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




回答5:


Because the table holds users. Its just the convention.



来源:https://stackoverflow.com/questions/10078139/ruby-on-rails-plural-controller-and-singular-model-convention-explanation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!