如何一个Hash对象添加到ActiveRecord类?尝试,但迁移失败 [英] How to add a Hash object to an ActiveRecord class? Tried but migration fails
本文介绍了如何一个Hash对象添加到ActiveRecord类?尝试,但迁移失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的ActiveRecord类用户
来包含选项(一串一串的键值),所以我写了:
I want my ActiveRecord class User
to contain options (a bunch of string key-values), so I wrote:
rails generate migration AddOptionsToUser options:Hash
它产生的:
class AddOptionsToUser < ActiveRecord::Migration
def self.up
add_column :users, :options, :Hash
end
def self.down
remove_column :users, :options
end
end
我还添加了此行我的课用户
:
serialize :options, Hash
但迁移失败:
But the migration fails:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Hash' at line 1: ALTER TABLE `users` ADD `options` Hash
我是新来的Rails,什么是存储一串字符串键值在ActiveRecord类常用的方法是什么?
I am new to Rails, what is the usual way to store a bunch of string key-values in an ActiveRecord class?
推荐答案
让Ruby对象的ActiveRecord模型,你应该使用连载
方法,类里面的属性该属性链接
To have ruby object as an attribute of the ActiveRecord model you should use serialize
method inside your class for that attribute link
这篇关于如何一个Hash对象添加到ActiveRecord类?尝试,但迁移失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文