Separate CanCan Role Model

I followed this tutorial on implementing a separate role model in CanCan . When a User, tries to register this error, is created at creation Assignment.

User(#21477600) expected, got Symbol(#5785720)

I am using Devise generated Userwith the following before_savefunctions

class User < ActiveRecord::Base
.
.
.
 def create_profile
    profile = Profile.new :user_id => :id
  end

  def create_role
     Assignment.new :user => :id, :role => Role.find_by_role("user").id
  end
end

I want to use the user role for the user by default, but I'm obviously doing something wrong. How should this be implemented?

+3
source share
4 answers

There may be some more efficient ways to achieve this, but I cannot say without exact model associations.

In any case, I think this should work:

def create_role
  Assignment.new :user => self, :role => Role.find_by_role("user")
end

: user : user_id, . : . : role_id, .id find_by_role, : role, .id

+3

, , , :

EDIT:

. , "" "UserRole".

user.rb

#--
# Relationship
has_many :user_roles, :dependent => :destroy, :uniq => true
has_many :roles, :through => :user_roles, :uniq => true

#--
# Instance Method

# Determine if the user has a specified role
# You can find this method at: https://github.com/ryanb/cancan/wiki/Separate-Role-Model
# Written by Ryan Bates, I added the downcase though to detect 'Admin' vs 'admin'.
# Example:
#       user.has_role? :Admin
#       => true
def has_role?(role_sym)
  roles.any? { |role| role.name.underscore.to_sym == role_sym.downcase }
end

role.rb

#  id         :integer(4)      not null, primary key
#  name       :string(255)  

#--
# Relationship
has_many :user_roles, :dependent => :destroy, :uniq => true
has_many :users, :through => :user_roles, :uniq => true

user_role.rb

#  id         :integer(4)      not null, primary key
#  user_id    :integer(4)
#  role_id    :integer(4)

#--
# Relationship
belongs_to :user
belongs_to :role

.rb

def initialize(user)
  user ||= User.new                   # in case of a guest
  if user.has_role? :Admin            # The user is an Administrator
    can :manage, :all
  else
    can :read, :all
  end
end

, , -, - :

# Create Users
...

# Roles
admin = Role.create!(:name => "admin")
standard = Role.create!(:name => "standard")

# UserRoles :Admin
user1.roles << admin
user2.roles << standard

, user.roles < < [role_name], UserRole, user_id role_id.

+9

, -, .

DanneManne .

Assignment.new( :user_id=>self.id, :role_id => Role.find_by_role('user').id )

( Danne, imo)

- "", "". , , Role.find_by_name ( "" ). .

+2

-, , , .

-, :

class User < ActiveRecord::Base
  has_one :profile
  has_many :assignments
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

class Assignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end

, user.profile, user.build_profile user.create_profile. user_id . - .

Please note that before saving the user, he does not have an identifier. Therefore, you need to use either before_create :build_profile, or after_create :create_profile. The first of them will create a profile in memory, which will be automatically saved after the user is saved, the second is quite simple.

Similarly, reference will be made also: user.assignments.build user.assignments.create. Thus, the final code for the user will look something like this.

class User < ActiveRecord::Base
  has_one :profile
  has_many :assignments
  after_create :create_profile, :create_assignment

  def create_assignment
    assignments.create :role => Role.find_by_role("user")
  end
end
+2
source

All Articles