0
我試圖保存在每個字典模型(我希望能夠搜索標籤基本上)的術語數組。所以terms = [「apple」,「pear」,「fruit」]可能是我想要保存在Dictionary模型的每個實例上的術語數組的一個示例。以前,術語是序列化的文本列。然後我改變它來嘗試和利用Postgres數組數據類型如下。當我嘗試更新一個模型(例如dictionary_example1.terms = [「fruit」,「pineapple」],然後調用save)時,所有存儲的數組都是一個空數組[]。我究竟做錯了什麼?無法保存Rails 4.2數組在Postgres數組列
Model.rb
class Dictionary < ActiveRecord::Base
serialize :terms, Array
end
schema.rb
create_table "clinical_trials", force: :cascade do |t|
t.string "terms", array: true
end
遷移
class ChangeTermsToArray < ActiveRecord::Migration
def change
change_column :dictionaries, :terms, "varchar[] USING (string_to_array(terms, ','))"
end
end
我認爲序列化的列應該在文本字段? – 2015-03-30 23:16:01
謝謝,從模型中刪除「序列化」訣竅。 – Nona 2015-03-30 23:33:09