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的后续语言约定。这意味着一个模型重新presents单个用户, 而数据库表中包含很多用户。
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屋!
查看全文