RolifyCommunity / rolify

Role management library with resource scoping
https://rolifycommunity.github.io/rolify/
MIT License
3.16k stars 403 forks source link

"Roles is invalid" exception when using with Mongoid 6 and Ruby On Rails 5 #435

Open Zeben opened 7 years ago

Zeben commented 7 years ago

Hello everybody. I've got an terrible bug that makes roles creation impossible. There is my steps:

  1. Generate new Ruby On Rails project: rails new sample --skip-active-record and cd into sample directory;
  2. Add gems mongoid, bson_ext and rolify into Gemfile;
  3. Bundle it with bundle;
  4. Issue the command rails g mongoid:config;
  5. Create new model named User: rails g model user name password_digest. It works with ActiveModel::SecurePassword, that makes possible using has_secure_password;
  6. Issue the command rails g rolify Role User -o=mongoid;
  7. Start to use Rails console via rails c command;
  8. Try to add new user with any role (e.g. admin):
    user = User.new
    user.name = "Admin"
    user.password = "verysecurepassword"
    user.add_role :admin
    user.save!
  9. Get an exception:

    Mongoid::Errors::Validations: 
    message:
    Validation of User failed.
    summary:
    The following errors were found: Roles is invalid
    resolution:
    Try persisting the document with valid data or remove the validations.
        from /usr/lib/ruby/gems/2.3.0/gems/mongoid-6.0.3/lib/mongoid/persistable.rb:78:in `fail_due_to_validation!'
        from /usr/lib/ruby/gems/2.3.0/gems/mongoid-6.0.3/lib/mongoid/persistable/savable.rb:45:in `save!'
        from (irb):6
        from /home/skovo/.gem/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
        from /home/skovo/.gem/ruby/2.3.0/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
        ...

    My user.rb file:

    class User
    include Mongoid::Document
    rolify
    include ActiveModel::SecurePassword
    field :name, type: String
    field :password_digest, type: String
    has_secure_password
    end

    ... and role.rb file that was generated via rails g rolify Role User -o=mongoid:

    class Role
    include Mongoid::Document
    has_and_belongs_to_many :users
    belongs_to :resource, :polymorphic => true
    
    field :name, :type => String
    
    index({
    :name => 1,
    :resource_type => 1,
    :resource_id => 1
    },
    { :unique => true})
    
    validates :resource_type,
            :inclusion => { :in => Rolify.resource_types },
            :allow_nil => true
    
    scopify
    end

I'm using Mongoid 6.0.3, Rails 5.0.1, bson_ext 1.5.1, and Rolify 5.1.0. The error doesn't occures in project that uses Rails 4.2.1. Please help me to understand where the error can be. Tnanks in advance.

akarsh007 commented 7 years ago

I am facing a similar error while upgrading Mongoid from version 5 to 6. Did you get any solution to it @Zeben .

Thanks

durran commented 7 years ago

Mongoid 6 follows the new pattern of AR5 requiring a belongs_to relation to always require its parent (see https://github.com/rails/rails/pull/18937/files). So rolify would need to change to have it set or the generation should make it optional, ie:

belongs_to :resource, :polymorphic => true, :optional => true
akarsh007 commented 7 years ago

Thanks a lot Durran. Will try this and keep you posted. Hopefully would be able to resolve the bug.

Regards, Akarsh

On Thu, Mar 2, 2017 at 5:33 PM, Durran Jordan notifications@github.com wrote:

Mongoid 6 follows the new pattern of AR5 requiring a belongs_to relation to always require its parent (see https://github.com/rails/ rails/pull/18937/files). So rolify would need to change to have it set or the generation should make it optional, ie:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/RolifyCommunity/rolify/issues/435#issuecomment-283635695, or mute the thread https://github.com/notifications/unsubscribe-auth/ANDj0G_U8xxjTAqjwOmKzwFTV8pN-fVQks5rhq_7gaJpZM4Lt73i .

Zeben commented 7 years ago

Hello Durran. It works for me, thank you many times for the comment. Recently I've deleted this validation but I've tried to restore the line with :optional => true completion. It works fine now. But anyway Rolify still doesn't create correct role.rb template, even with new project creation. Will be fixed this behaviour in Rolify in near future? Thanks in advance.