转载 :http://blog.youkuaiyun.com/meteorlWJ/archive/2008/02/03/2079356.aspx
现在我们有一种新的migration文件中声明的格式。以前我们是这样写的:
Ruby代码 复制代码
而现在,我们可以这样写:
Ruby代码 复制代码
更多关于sexy migration:
现在你只需要这样写:
t.references :account也可以写成 t.belongs_to :account(和ActiveRecord::Base一样)
和ActiveRecord::Base的belongs_to一样,多态也同样支持。
现在我们有一种新的migration文件中声明的格式。以前我们是这样写的:
Ruby代码 复制代码
create_table :people do |t|
t.column, "account_id", :integer
t.column, "first_name", :string, :null => false
t.column, "last_name", :string, :null => false
t.column, "description", :text
t.column, "created_at", :datetime
t.column, "updated_at", :datetime
end 而现在,我们可以这样写:
Ruby代码 复制代码
create_table :people do |t|
t.integer :account_id
t.string :first_name, :last_name, :null => false
t.text :description
t.timestamps
end 更多关于sexy migration:
create_table :people do |t|
t.column, “account_id”, :integer
t.column, “first_name”, :string, :null => false
t.column, “last_name”, :string, :null => false
t.column, “description”, :text
t.column, “created_at”, :datetime
t.column, “updated_at”, :datetime
end 现在你只需要这样写:
create_table :people do |t|
t.references :account
t.string :first_name, :last_name, :null => false
t.text :description
t.timestamps
end t.references :account也可以写成 t.belongs_to :account(和ActiveRecord::Base一样)
和ActiveRecord::Base的belongs_to一样,多态也同样支持。
create_table :taggings do |t|
t.integer :tag_id, :tagger_id, :taggable_id
t.string :tagger_type
t.string :taggable_type, :default => 'Photo'
end
本文介绍了一种在 Rails 中简化迁移文件声明的新方法。通过使用更简洁的语法,如 t.references 和 t.belongs_to 来替代传统的 t.column,使得数据库表定义更为直观且易于维护。

被折叠的 条评论
为什么被折叠?



