【发布时间】:2015-03-30 23:12:01
【问题描述】:
我正在尝试在每个 Dictionary 模型上保存一组术语(我希望能够基本上搜索标签)。所以 terms=["apple", "pear", "fruit"] 可能是我试图在 Dictionary 模型的每个实例上保存的术语数组的一个示例。以前,terms 是一个序列化的文本列。然后我将其更改为尝试利用 Postgres 数组数据类型,如下所示。当我尝试更新模型时(例如,dictionary_example1.terms = ["fruit", "pineapple"] 然后调用保存),存储的只是一个空数组 []。我做错了什么?
模型.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
【问题讨论】:
-
我认为序列化的列应该在文本字段中?
-
谢谢,从模型中删除“序列化”就行了。
标签: ruby-on-rails arrays postgresql ruby-on-rails-4