将哈希添加到ActiveRecord哈希

时间:2013-10-25 02:55:26

标签: ruby ruby-on-rails-3 activerecord rails-activerecord

我看了How to add a Hash object to an ActiveRecord class? Tried but migration fails并按照那里的格式。

我试过了:

class AddTestResponsesToSurveys < ActiveRecord::Migration
    def change
        add_column :surveys, :responses, :hash
    end
end

当我运行rake db:migrate时,我在schema.rb文件中收到错误消息:

# Could not dump table "surveys" because of following StandardError
#   Unknown type 'hash' for column 'responses'

我做错了什么?

1 个答案:

答案 0 :(得分:1)

使用列类型文本生成迁移

class AddTestResponsesToSurveys < ActiveRecord::Migration
    def change
        add_column :surveys, :responses, :text
    end
end

在您的Survey模型中,添加此

serialize :responses, Hash