2013-01-02 148 views
0

這是我的移民文件:耙分貝:遷移錯誤錯誤的參數數目(5 4)

class AddSeoMetaInfoToArticles < ActiveRecord::Migration 
    def self.up 
    add_column :articles, :seo_title, :string, { :default => "", :null => false } 
    add_column :articles, :seo_description, :string, { :default => "", :null => false } 
    add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false } 
    end 

    def self.down 
    remove_column :articles, :seo_keywords 
    remove_column :articles, :seo_description 
    remove_column :articles, :seo_title 
    end 
end 

當我嘗試運行「耙分貝:遷移」我收到以下錯誤

$ rake db:migrate 
AddSeoMetaInfoToArticles: migrating ======================================= 
-- add_column(:articles, :seo_title, :string, {:default=>"", :null=>false}) 
    -> 0.0341s 
-- add_column(:articles, :seo_description, :string, {:default=>"", :null=>false}) 
    -> 0.0100s 
-- add_column(:articles, :seo_keywords, :string, :string, {:default=>"", :null=>false}) 
rake aborted! 
An error has occurred, this and all later migrations canceled: 

wrong number of arguments (5 for 4) 

Tasks: TOP => db:migrate 
(See full trace by running task with --trace) 

我比較新的軌道,我不知道我做錯了什麼。這是Rails 3.0.9和Postgres數據庫,如果這有所作爲。

回答

2
add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false } 

:string兩次,所以你最終被傳遞,而不是4

5個參數,您可能還需要考慮寫你與change遷移 - 你的遷移相當於

class AddSeoMetaInfoToArticles < ActiveRecord::Migration 
    def change 
    change_table :articles do |t| 
     t.string :seo_title, :default => "", :null => false 
     t.string :seo_description, :default => "", :null => false 
     t.string :seo_keywords, :default => "", :null => false 
    end 
    end 
end 

我覺得在眼睛上更容易。它還有一個好處,你可以將:bulk => true傳遞給change_table,它將所有3列添加到1個alter table語句中,這通常要快得多。

這兩種方式當然會起作用。

+0

/facepalm有時你只需要另一雙眼睛來檢查它。 – JonathanW

0

你給:string參數兩次在這一行:

add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }