I have one particular user in my rails app for which I can't log in anymore, the password is always invalid, even though I changed it manually. Here's what I do in rails console :
> me = User.find(10)
> me.password = '123456789'
> me.save
(0.3ms) BEGIN
User Exists (0.6ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'myemail#gmail.com' AND "users"."id" != 10) LIMIT 1
SQL (0.7ms) UPDATE "users" SET "encrypted_password" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["encrypted_password", "$2a$10$mrhWiOT3pu6YldtYRD/bC.wuqPthyfJhiqdGkYv14xCafVQNTodWG"], ["updated_at", "2016-08-08 10:43:34.715229"], ["id", 10]]
(31.3ms) COMMIT
=> true
> me.valid_password?('123456789')
=> nil
This is only with this particular user id 10. I do the exact same thing with any other user it works. What could be wrong ?
EDIT : I tried also with password confirmation but that's not the issue. As I said, the exact manipulation works fine with any user except this one of ID 10
EDIT 2 : I found the solution in this thread : Rails/Devise: valid_password? method returns nil
I think probably you need to set password_confirmation as well, Try below code.
> me = User.find(10)
> me.password = '123456789'
> me.password_confirmation = '123456789'
> me.save
Related
I am trying to assign parent as superadmin. when i run the command parent.toggle!(superadmin) it doesnt assign the parent as superadmin.
I am not able to locate the error
irb(main):004:0> parent.toggle!(:superadmin)
(0.2ms) begin transaction
Parent Update (0.6ms) UPDATE "parents" SET "updated_at" = ?, "superadmin" = ? WHERE "parents"."id" = ? [["updated_at", "2018-06-20 17:50:31.273816"], ["superadmin", 1], ["id", 1]]
Parent Update (0.2ms) UPDATE "parents" SET "updated_at" = ?, "superadmin" = ? WHERE "parents"."id" = ? [["updated_at", "2018-06-20 17:50:31.280343"], ["superadmin", 0], ["id", 1]]
(1.8ms) commit transaction
=> true
it seems that the transaction is beign done, remember that toggle doesn't assign true, it changes the false to true a viceversa, depending on the current value that it has.
on the example you are sending, it's changed to true at first but then again to false. you are doing it right talking about code. but maybe there is a callback that changes again the value to false before saving it. Check on your model if there are callbacks or if you have some rules for this.
I realize this may not be "best practice" but this is sort of a temporary fix/experiment. I'm trying to update a record on Heroku using rails console but whenever I save it just rolls back.
UserAdmin = User.find_by(email: "User#example.com")
UserAdmin.admin = true
UserAdmin.save
The result is:
(0.6ms) BEGIN
(0.6ms) BEGIN
User Exists (0.6ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('User#example.com') AND "users"."id" != 3) LIMIT 1
User Exists (0.6ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."email") = LOWER('User#example.com') AND "users"."id" != 3) LIMIT 1
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."user_name") = LOWER('example') AND "users"."id" != 3) LIMIT 1
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE (LOWER("users"."user_name") = LOWER('example') AND "users"."id" != 3) LIMIT 1
(0.4ms) ROLLBACK
(0.4ms) ROLLBACK
Am I going about this wrong? Is there any particular reason the record is not saving on Heroku?
What do you get if you do:
user_admin = User.find_by(email: "User#example.com")
user_admin.admin = true
user_admin.valid?
user_admin.errors.full_messages
(I realize this is not an answer yet, but posted it this way because it would be too messy as a comment.)
When I'm creating a new column through a migration helper and setting the value through ruby-active-record-model-code. even through the changes are valid and the ruby object data looks good the new attribute is not saved through ActiveRecord after all.
Thats what i'm doing in detail:
I'm adding the new column to my schema for the user model:
add_column :users, :billing_day_of_month, :integer
right after this, also in my migration file, I want to fill this new column following a simple ruby formula:
User.customers.each do |u|
day = nil
if u.last_charged_at
day = u.last_charged_at.day
else
day = u.plan_started_at.day
end
u.billing_day_of_month = day
puts "precheck #{u.id} #{u.valid?}"
if u.save
puts "saved #{u.id} -> #{u.billing_day_of_month}"
else
puts "not saved #{u.errors.inspect}"
end
end
All outputs look very promising:
precheck 399 true
saved 399 -> 22
precheck 414 true
saved 414 -> 9
precheck 203 true
saved 203 -> 8
precheck 439 true
saved 439 -> 21
Unfortunately, the values in the model are not being saved via ActiveRecord:
SQL (0.4ms) UPDATE "users" SET "updated_at" = $1 WHERE "users"."id" = $2 [["updated_at", "2016-01-15 13:01:04.966681"], ["id", 399]]
SQL (0.4ms) UPDATE "users" SET "updated_at" = $1 WHERE "users"."id" = $2 [["updated_at", "2016-01-15 13:01:04.969648"], ["id", 414]]
SQL (0.4ms) UPDATE "users" SET "updated_at" = $1 WHERE "users"."id" = $2 [["updated_at", "2016-01-15 13:01:04.972547"], ["id", 203]]
SQL (0.4ms) UPDATE "users" SET "updated_at" = $1 WHERE "users"."id" = $2 [["updated_at", "2016-01-15 13:01:04.975492"], ["id", 439]]
So I digged around and found that User.attribute_names is missing the new attribute name billing_day_of_month. When I use the rails console after this migration, the User.attribute_names does contain the new attribute name.
Btw at the time of migration, the schema.rb does contain the new column.
Anyone have a clue what I'm missing here? - I already used new cols within the same migration through a model and it always worked.
Try adding User.reset_column_information before the update loop. See http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-reset_column_information for more details.
I'm trying to set user's (admin) password from Rails console:
bundle exec rails console
> Spree::User.first.email
=> "admin#mysite.com"
> Spree::User.first.encrypted_password
Spree::User Load (1.1ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
=> "4ec556............................................."
> Spree::User.first.password='spree123'
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
=> "spree123"
> Spree::User.first.password_confirmation='spree123'
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
=> "spree123"
> Spree::User.first.save!
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
(0.3ms) BEGIN
(1.3ms) SELECT COUNT(DISTINCT "spree_users"."id") FROM "spree_users" LEFT OUTER JOIN "spree_roles_users" ON "spree_roles_users"."user_id" = "spree_users"."id" LEFT OUTER JOIN "spree_roles" ON "spree_roles"."id" = "spree_roles_users"."role_id" WHERE "spree_roles"."name" = 'admin'
(0.3ms) COMMIT
=> true
> Spree::User.first.encrypted_password
Spree::User Load (1.0ms) SELECT "spree_users".* FROM "spree_users" LIMIT 1
=> "1bc15d.............................................."
So far so good. It looks like the new password for the user has been changed and commited to the database. However when I try to log in later with a web client and using the new password, it fails with invalid identity/password message.
I even tried to update password with Spree::User.first.reset_password!('spree123', 'spree123') but sill cann't sign in.
Rails 3.2.12
Spree 1.3.2
Any idea what am I doing wrong ? How to properly set a new password ?
Thanks.
The problem is that every time you're doing Spree::User.first it's reloading the record from the database. This means you are setting the value on one instance of the record, reloading it, and then saving the reloaded model that hasn't actually changed. An easy way around this is to create a local instance variable containing the record and update that instead:
user = Spree::User.first
user.password='spree123'
user.password_confirmation='spree123'
user.save!
Spree::User.first.update_attributes(password: 'password')
The update function is working within my ruby console as shown below, but the updates aren't being reflected in the database for this particular user.
For all other users it works perfectly, and using update_all works, but also updates all other users. Any idea why?
ruby-1.9.2-p290 :002 > User.update(51, :introduction => "testupdate")
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 51]]
(0.3ms) UPDATE "users" SET "introduction" = 'testupdate', "updated_at" = '2011-09-13 18:53:25.896711' WHERE "users"."id" = 51
=> #<User id: 51, email: "example#email.com", introduction: "testupdate", created_at: "2011-09-13 18:45:18", updated_at: "2011-09-13 18:53:25">
FYI The user I'm trying to update is the only user created manually via log-in, the others are created as dummy accounts via seeds.rb
I accidentally had an after_save in the User model that was actually causing a validation failure. Thanks #meagar