Ruby on rails create_表中的block变量在Rails迁移中来自何处?
查看创建的迁移:Ruby on rails create_表中的block变量在Rails迁移中来自何处?,ruby-on-rails,Ruby On Rails,查看创建的迁移: rails generate model User email:string password:string class CreateUsers
rails generate model User email:string password:string
class CreateUsers
从“开始Rails 4”一书中可以看出:“这是一种标准的迁移费用。在更改定义中,使用create_table方法创建一个新的users表。新的表对象被转换到块变量t,在该块变量t上,您调用string方法来创建每个列。”
我想更详细地解释块变量t的确切来源。它是由create_table方法返回的吗(我想不是,但我想不出其他任何东西)?来自文档:
class CreateUsers < ActiveRecord::Base
def change
create_table :users do |t|
t.string :email
t.string :password
t.timestamps
end
end
end
因此,您看到的
t
实际上是TableDefinition
类的对象。你可以在这里阅读更多关于这个类的内容:一个块是你在do
和end
之间看到的。本例中的块变量是在这里声明的create|u table:用户在该行的
之间执行| t |
。我想这就是答案
# create_table() passes a TableDefinition object to the block.
# This form will not only create the table, but also columns for the
# table.