我希望我的ActiveRecord類User
包含選項(一串字符串的鍵值),所以我寫了:如何將哈希對象添加到ActiveRecord類?試過,但遷移失敗
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
我還添加了這種在線上我的課User
:
serialize :options, Hash
但遷移失敗:
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類中存儲一串字符串鍵值的常用方法是什麼?
我已經有了:'serialize:options,Hash' in class User。但錯誤 –
嘗試改變從'Hash'到'text'或'string'的遷移列的類型 – Bohdan
如果我使用rails生成遷移AddOptionsToUser選項:text',它確實可行。謝謝! –