tohare
tohare

Reputation: 33

Can't update user in Rails_Admin for and model with belongs_to :user

I have 2 scenarios but both resolve to the same result. Both scenarios involve trying to update records via Rails_admin with both models having a "belongs_to :user" association.

Scenario 1: I need to update a Video record which belongs to a User. If I try to change the user this video is assigned, the user_id will always change to the user_id of the admin logged in to Rails_Admin. The user_id will change to the currently logged in Admin regardless of the field I try to edit.

Scenario 2: I need to update a credit amount on a Profile record which belongs to a User. Once again, no matter what field I change even if the User is not visible, the user_id changes to the currently logged in Admin's user_id.

Suggestions?

Upvotes: 2

Views: 853

Answers (2)

domangi
domangi

Reputation: 11

I had the same issue and i solved it by changing my abilities, in order to avoid ability overlap. That means, if a user is an admin, then he will not have abilities setted for other users.

Example for overlap that was causing the error for me:

user ||= User.new # guest user (not logged in)

can :manage, Video, user: user
if user.admin?
  can :manage, Video
end

This should be changed to:

user ||= User.new # guest user (not logged in)

if user.admin?
  can :manage, Video
else
  can [:crud], Video, :user_id => user.id
end  

Upvotes: 1

Benoit B.
Benoit B.

Reputation: 3661

Sounds like you have an issue with your cancan configuration.

check for lines like:

can :manage, Video, user: user

In that case, RailsAdmin will override any value for user_id to current_user.

Upvotes: 0

Related Questions