Ruby on Rails 复数(控制器)和单数(模型)约定 - 解释 [英] Ruby on Rails plural (controller) and singular (model) convention - explanation
本文介绍了Ruby on Rails 复数(控制器)和单数(模型)约定 - 解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据 Ruby on Rails 约定,控制器名称采用复数形式,而模型名称采用单数形式.示例:一个用户控制器,但一个用户模型.
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
现在打开迁移文件
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 遵循语言约定.这意味着一个模型代表一个用户,而一个数据库表由许多用户组成.
Ruby on Rails follow linguistic convention. That means a model represents a single user, whereas a database table consists of many users.
这篇关于Ruby on Rails 复数(控制器)和单数(模型)约定 - 解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文