2014-04-03 66 views
0

我想通過使用活動記錄從表中刪除列。請找到以下代碼片段如何刪除活動記錄中的表列

require "active_record" 
require 'sqlite3' 
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'test_one') 

class Account < ActiveRecord::Base 
table_name = "AccountBean" 
primary_key = "process_id" 
remove_column "xxx" // I need this type of method to remove column "xxx" from accounts table 
end 

ActiveRecord中是否有任何滿足此要求的類方法?

回答

0

我猜ActiveRecord認爲應該使用遷移來完成對結構的更改。

如果您確實需要,可以使用rails用於遷移到例如刪除列 - like here

我不建議這樣做:)

ActiveRecord::Base.connection.remove_column("persons", "first_name")

在一類,看起來像:

class Account < ActiveRecord::Base 
table_name = "AccountBean" 
primary_key = "process_id" 
connection.remove_column(table_name, "xxx") 
end 
+0

是的,但使用的連接方法,即使我們知道我們在表代表類(帳戶)是有點奇怪 – pramod

+1

@pramod - 方法'remove_column'只適用於遷移,因此你不能指望有一個很好的簡單的API來改變你的表結構從'行動iveRecord'模型:) – Magnuss