Open afuno opened 5 years ago
I also want to add that if I create a user without roles, and then manually write in the console this:
User.first.add_role(:manager)
Then everything will be fine. One role and one relationship will be created.
What the hell guys... 😀 Why did this fix my problem?
class User < ApplicationRecord
devise :database_authenticatable,
:registerable,
:confirmable,
:lockable,
:recoverable,
:rememberable,
:validatable,
:trackable
has_many :users_roles
has_many :roles,
through: :users_roles,
dependent: :destroy
rolify
after_create :assign_default_role
private
def assign_default_role
return if is_manager?
Rails.logger.debug 'LOG START'
add_role :manager
Rails.logger.debug 'LOG END'
end
end
I think you can remove
has_many :users_roles
has_many :roles, through: :users_roles
and just hold rolify (at start), because it will do the job. roles has_many users through users_roles can stay in the Role model (no rolify there).
Also, you are starting your logger after to create User, if you start logger before you should see more interesting actions happening (maybe).
I am having the issue, the above fixes did not work for me. Looks like this issue is not closed yet?
@kapso there is no issue there. which issue do you have from which code ?
@jerome-diver when trying to add_role
in after_create
callback creates a duplicate record in user_roles
. I moved the add_role
out of the callback, and now there's no dup record.
@kapso, please, show your model source code for user. maybe paste it there... if there is double, it is because there is something happening two times because of wrong model (or dirty controller is also possible). Actually rofily lib seems to works pretty well on my side.
I can confirm, having similar issue as poster. One call to add_role
but multiple queries executed. However I'm not convinced this is a rolify issue and may be related to activerecord autosave for HABTM records.
Logs:
[DEBUG] [ActiveRecord] User::HABTM_Roles Create
INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (?, ?)
[INFO] User role added (manager)
...
[DEBUG] [ActiveRecord] User::HABTM_Roles Create
INSERT INTO `users_roles` (`user_id`, `role_id`) VALUES (?, ?)
This seems to only occur on new users where add_role
is called within the after_create
callback.
@skukx did you figure what the problem was? I'm experiencing the exact same issue with Rails 6. My User model is pretty straightforward, so not sure what's going on
class User < ApplicationRecord
include Clearance::User
validates :email, uniqueness: true
validates :first_name, :last_name, presence: true
after_create :assign_default_role
rolify
has_paper_trail
default_scope { order(email: :asc) }
def assign_default_role
self.add_role(:user) if self.roles.blank?
end
def role_names
roles.collect { |r| r.name }
end
def is_admin?
self.has_role? :admin
end
def is_admin_or_manager?
self.has_role?(:admin) || self.has_role?(:manager)
end
end
@yorch I imagine you've fixed this, but I just hit this and think I've gotten past it by moving the after_create
callback underneath the rolify
call.
@yorch I imagine you've fixed this, but I just hit this and think I've gotten past it by moving the
after_create
callback underneath therolify
call.
This worked for me, thank you wooly.
It is clear that there is a bug Rolify around this issue, but the bug may indeed be related to ActiveRecord HABTM records, as mentioned by skukx
In my case the ordering of the rolify/after_create didnt make any difference, but switching to after_create_commit
did fix the issue
I created a Rails 5.2.3 application. Next, I added Devise and Rolify.
In the application, I need to automatically assign a role to new users. For this, I wrote the following code (BTW, the similar code is in your documentation):
All User model code:
Next, I fill out the Devise registration form and send it:
In the log you need to pay attention to the
LOG END
. Before and after it you can see these lines:Creating a connection comes from the same place:
Moreover, the second creation is called before the
LOG END
, but actually occurs after theLOG END
:Why is this happening?
If I delete this line, no roles will be created and linked: