`

Active Record: Sexy migrations

阅读更多
转载 :http://blog.csdn.net/meteorlWJ/archive/2008/02/03/2079356.aspx

现在我们有一种新的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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics