2016-03-22 7 views
15

मैं रोलिफ़ाइम मणि सेट अप करने का प्रयास कर रहा हूं और मैं कंसोल में किसी उपयोगकर्ता को भूमिका निभाने में एक समस्या में भाग रहा हूं।तालिका त्रुटि को हल करें (user.add_role: admin अज्ञात कुंजी त्रुटि)

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

मैं cancancan और rolify साथ चिंतन चल रहा हूँ:

यहाँ मेरी त्रुटि है। मैं सदस्यता भुगतान समर्थन के लिए Koudoku मणि भी चला रहा हूँ। मुझे संदेह है कि यह त्रुटि इस तथ्य के कारण हो सकती है कि मेरी "सब्सक्रिप्शन" तालिका में "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 

धन्यवाद।

उत्तर

39

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 पैरामीटर रेल संस्करण 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 
+0

यह काम करता था। धन्यवाद @pranavpr! –

+3

धन्यवाद! यह वास्तव में एक अच्छा मणि है, लेकिन निराश है कि इस तरह की चीज बॉक्स को संभाला नहीं जाता है, विशेष रूप से यह देखते हुए कि कितने ऐप्स रेल संस्करण का उपयोग करते हैं <5 – user2490003

संबंधित मुद्दे