简体   繁体   中英

Rails Mountable Engine with CanCan

I'm working on a blog engine and I wan't to setup cancan. I found some ways to do it:

forem doest the thing by adding an admin field in the host user model, but having multiple roles would require many fields, and that is definitely not the way to do it

I saw some exames like this but to do it I would need to have my host app user model having has_many relations to my engine models, like this

class User < ActiveRecord::Base
    attr_accessible :name
    has_many Blogcms::RoleUser
    has_many Blogcms::Role, :through => Blogcms::RoleUser
end

I don't really know if this is the right way to do it, but this wont work because beeing a isolated engine the engine models will be invisible

has someone tried this? sorry for my english

EDIT

I found a way around, not having to search through the User model and just setting up the relations in my Role model

module Blogcms
  class Role < ActiveRecord::Base

    has_many :role_user
    has_many :user, :class_name =>  Blogcms.user_class, :through => :role_user

    attr_accessible :name

    # Return all the roles for a user
    def self.roles_for_user(user)
        joins(:user).where('users.id' => user.id)
    end

  end
end

like that my roles_for_user methods returns all the roles for a user

I found a way around, not having to search through the User model and just setting up the relations in my Role model

module Blogcms
  class Role < ActiveRecord::Base

    has_many :role_user
    has_many :user, :class_name =>  Blogcms.user_class, :through => :role_user

    attr_accessible :name

    # Return all the roles for a user
    def self.roles_for_user(user)
        joins(:user).where('users.id' => user.id)
    end

  end
end

like that my roles_for_user methods returns all the roles for a user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM