3

私が作成した次のモデルがあります。

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :password_digest
      t.string :location_type
      t.integer :location_id
      t.integer :categories, array: true, default: '{}'

      t.timestamps
    end
    add_index :user, :email, unique: true
  end
end

また、pg 配列パーサー gem を Gemfile に追加しました。

問題は、ユーザーを作成するたびに、カテゴリが不明な属性であると通知されることです。

User.create(name: "Bob", email: "bob@gmail.com", 
password: "password", password_confirmation: "password", categories: [1, 2])

The Error:

unknown attribute: categories

何が問題で、どうすれば修正できますか?

アップデート:

実行した後rake db:drop db:create db:migrate、この新しいエラーに遭遇しました。

PG::Error: ERROR:  column "categories" is of type integer[] but default expression is of type integer
HINT:  You will need to rewrite or cast the expression.
4

1 に答える 1