Reputation: 3659
I have a simple problem. I want to change some field value for my User.find(1)
in rails console.
I tried:
u = User.find(1)
u.update_attributes(roles_mask: 3)
And got false
returned. When I check u.errors.full_messages
, I see that it's because there is a problem with password validation from has_secure_password
. How can I update it manually in the console?
Upvotes: 44
Views: 51804
Reputation: 597
You can have something like this
Post.find(1).comments.first.update(body: "hello")
or
@u = Post.find(1)
@u.comments.first.update(body: "hello")
where comments is the name of the comment table, body is the name of column in the comment table.
This applies when you have nested attribute
post.rb
has_many: comments
accept_nested_attributes_for: comment
comment.rb
belongs_to: post
Upvotes: 4
Reputation: 21863
You can try update_attribute(:roles_mask, 3)
or update_column(:roles_mask, 3)
.
Upvotes: 5
Reputation: 2385
You have to authenticate the user first then you can update the user have a look here
u = User.find(1)
u.authenticate("password")
u.update_attributes(roles_mask: 3)
Or if you want to skip the validations you can do as follow;
u = User.find(1)
u.update_attribute :roles_mask, 3
Upvotes: 11
Reputation: 29599
if you want to bypass validation, use
# skip validations but run callbacks
u.update_attribute :roles_mask, 3
or
# do the update on the sql so no validation and callback is executed
u.update_column :roles_mask, 3
Upvotes: 72