Ошибка Rolify Table (user.add_role: admin Неизвестная ошибка ключа)

Я пытаюсь настроить роллинг-камень, и у меня возникает проблема с назначением роли пользователю на консоли.

Здесь моя ошибка:

2.2.1 :007 > user.add_role :admin
ArgumentError: Unknown key: :optional.

Я работаю с cancancan и закатываю. Я также использую камень Koudoku для поддержки подписки. Я подозреваю, что эта ошибка может быть вызвана тем фактом, что таблица "subscriptions" также имеет столбец "user_id". Есть ли что-нибудь, что я могу сделать, чтобы исправить эту проблему?

Здесь моя схема.

create_table "subscriptions", force: :cascade do |t|
t.string   "stripe_id"
t.integer  "plan_id"
t.string   "last_four"
t.integer  "coupon_id"
t.string   "card_type"
t.float    "current_price"
t.integer  "user_id"
t.datetime "created_at",    null: false
t.datetime "updated_at",    null: false
end

create_table "users", force: :cascade do |t|
t.string   "email",                  default: "", null: false
t.string   "encrypted_password",     default: "", null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,  null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at",                          null: false
t.datetime "updated_at",                          null: false
t.string   "first_name"
t.string   "string"
t.string   "last_name"
end

 add_index "users", ["email"], name: "index_users_on_email", unique: true
 add_index "users", ["reset_password_token"], name:    
 "index_users_on_reset_password_token", unique: true

create_table "users_roles", id: false, force: :cascade do |t|
t.integer "user_id"
t.integer "role_id"
end

add_index "users_roles", ["user_id", "role_id"], name:   "index_users_roles_on_user_id_and_role_id"

end

Спасибо.

Ответ 1

Генератор ролей Rolify генерирует модель ролей со следующим кодом:

class Role < ActiveRecord::Base


has_and_belongs_to_many :users, :join_table => :users_roles

  belongs_to :resource,
             :polymorphic => true,
             :optional => true

  validates :resource_type,
            :inclusion => { :in => Rolify.resource_types },
            :allow_nil => true

  scopify
end

Параметр :optional => true поддерживается в Rails версии 5 и выше. Чтобы обойти эту проблему, просто удалите эту строку из своей модели ролей, и вам должно быть хорошо идти. Ниже приведен окончательный код для справки:

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => :users_roles

  belongs_to :resource,
             :polymorphic => true

  validates :resource_type,
            :inclusion => { :in => Rolify.resource_types },
            :allow_nil => true

  scopify
end