Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolify Table Error (user.add_role :admin Unknown Key Error)

I'm attempting to setup the rolify gem and I'm running into an issue assigning a role to a user in the console.

Here's my error:

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

I'm running devise with cancancan and rolify. I'm also running the Koudoku gem for subscription payment support. I'm suspecting this error might be caused by the fact that my "subscriptions" table also has a "user_id" column. Is there anything I can do to correct this issue?

Here's my schema.

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

Thanks.

like image 973
John Gerard Avatar asked Mar 22 '16 01:03

John Gerard


1 Answers

The Rolify role generator generates Role model with following code:

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

The :optional => true parameter is supported in Rails version 5 and above. To get around this issue, just delete that line from your Role model and you should be good to go. Below is final code for your reference:

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
like image 63
Pranav Prakash Avatar answered Sep 30 '22 17:09

Pranav Prakash