Updating a Heroku record with rails console - ruby-on-rails

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.)

Related

Cannot update status of table specific instance because rollback is taking place

I have a table subscription with a column status. In my subscriptions controller I have a method accept_player that is supposed to update the subscription.status to "confirmed!"
def accept_player
#subscription = Subscription.find(params[:subscription_id_accept_player])
#subscription.status = "confirmed!"
#subscription.save
authorize #subscription
redirect_to tournament_subscriptions_path(#subscription.tournament)
end
unfortunately every time I try to trigger that method, a rollback seem to take place:
Started POST "/accept_player/39" for ::1 at 2015-07-08 22:01:21 +0100
ActiveRecord::SchemaMigration Load (12.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
/Users/davidgeismar/code/davidgeismar/tennis-match/app/controllers/subscriptions_controller.rb:141: warning: duplicated key at line 155 ignored: "CardType"
Processing by SubscriptionsController#accept_player as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"aas8OPHBpvPwNbbmx/SVipsRM+eKo63nuVilMroxKcU9HRVonjSqEuH7aLY91gFi9PHMUsUqRqk7qhnv2m4L/A==", "subscription_id_accept_player"=>"39", "commit"=>"Confirmer ce Joueur", "subscription_id"=>"39"}
User Load (13.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Subscription Load (11.6ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = $1 LIMIT 1 [["id", 39]]
(5.7ms) BEGIN
Subscription Exists (0.8ms) SELECT 1 AS one FROM "subscriptions" WHERE ("subscriptions"."user_id" = 20 AND "subscriptions"."id" != 39 AND "subscriptions"."tournament_id" = 9) LIMIT 1
(12.6ms) ROLLBACK
Tournament Load (2.4ms) SELECT "tournaments".* FROM "tournaments" WHERE "tournaments"."id" = $1 LIMIT 1 [["id", 9]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
Redirected to http://localhost:3000/tournaments/9/subscriptions
Completed 302 Found in 246ms (ActiveRecord: 79.7ms)
Any ideas about what might be going wrong here ?
This code:
authorize #subscription
is probably causing the rollback. If you're in dev mode, just comment it out, reload!, and try to manually add a record and see if that's the cause.

Ruby on Rails: dependent object destroyed when transfered from guest user to registered user

Here is my problem:
I'm using Devise's guest_user, that contains a logging_in method to transfer guest_user parameters to the registered user when he logs in. So in my case, the user has_many periods, dependent: :destroy, so here is the logging_in method:
def logging_in
guest_periods = guest_user.periods.all
guest_periods.each do |p|
p.user_id = current_user.id
p.save!
end
current_user.latest_entry = guest_user.latest_entry
current_user.is_in_zone = guest_user.is_in_zone
current_user.save
end
However, when a guest_user logs in, his periods gets destroyed instead of being transfered. Here is the log:
Started GET "/" for ::1 at 2015-05-11 00:18:03 +0300
Processing by WelcomeController#index as HTML
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 24]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 23]]
Period Load (0.3ms) SELECT "periods".* FROM "periods" WHERE "periods"."user_id" = $1 [["user_id", 23]]
(0.2ms) BEGIN
CACHE (0.0ms) SELECT "periods".* FROM "periods" WHERE "periods"."user_id" = $1 [["user_id", 23]]
SQL (0.8ms) UPDATE "periods" SET "user_id" = $1, "updated_at" = $2 WHERE "periods"."id" = $3 [["user_id", 24], ["updated_at", "2015-05-10 21:18:03.863162"], ["id", 170]]
(0.9ms) COMMIT
(0.2ms) BEGIN
SQL (2.1ms) UPDATE "users" SET "is_in_zone" = $1, "latest_entry" = $2, "updated_at" = $3 WHERE "users"."id" = $4 [["is_in_zone", "t"], ["latest_entry", "2015-05-04"], ["updated_at", "2015-05-10 21:18:03.875572"], ["id", 24]]
(15.8ms) COMMIT
(0.5ms) BEGIN
SQL (0.3ms) DELETE FROM "periods" WHERE "periods"."id" = $1 [["id", 170]]
SQL (0.7ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 23]]
(1.2ms) COMMIT
So we can see that the transfer is done, but then in the end, the periods are destroyed anyway. They should not be, as they are not belonging to the user to be destroyed any more.
Why is it happening?
Even though Period#user_id has changed, guest_user.periods is still loaded in memory and is what gets destroyed when you destroy the guest user. If you guest_user.reload, its associations will clear out and it becomes safe to destroy. You could also guest_user.periods(true) to force reload of just the periods.
Another option is:
guest_user.periods.update_all(user_id: current_user.id)
This executes a single query to perform the update, which will be nice if there are a lot of periods, and also doesn't load the guest_user.periods association, so it will load fresh during the destroy and find the correct empty set.

Spree/Rails user password reset

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')

Failed to update email address using devise only in production environment

This one is driving me absolutely crazy. I tested updating user's email address on dev and staging environments, and it all worked fine. It updates unconfirmed_email field and sends out a confirmation email address.
However, only in production environment, it fails!
In dev/staging environment, I see the following statements when a user updates his/her email address.
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
Profile Load (0.3ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 2 LIMIT 1
(0.1ms) BEGIN
User Exists (0.6ms) SELECT 1 FROM "users" WHERE ("users"."email" = 'aaaa#yahoo.com' AND "users"."id" != 2) LIMIT 1
(0.3ms) UPDATE "users" SET "unconfirmed_email" = 'aaaa#yahoo.com', "updated_at" = '2012-08-27 04:22:10.470329' WHERE "users"."id" = 2
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'jUcfXqMqDjeEsJ3TEKws' LIMIT 1
(0.3ms) UPDATE "users" SET "unconfirmed_email" = 'aaaa#yahoo.com', "updated_at" = '2012-08-27 04:22:10.470329', "confirmation_token" = 'jUcfXqMqDjeEsJ3TEKws', "confirmation_sent_at" = '2012-08-27 04:22:10.473264' WHERE "users"."id" = 2
However, in production, I see the following.
User Load (6.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
Profile Load (2.1ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 5 LIMIT 1
(1.2ms) BEGIN
User Exists (1.3ms) SELECT 1 FROM "users" WHERE ("users"."email" = 'aaaa#yahoo.com' AND "users"."id" != 5) LIMIT 1
(1.2ms) ROLLBACK
One more odd thing. I could update the email address manually in rails console.....
1.9.3p194 :005 > u.update_attributes(:email => "aaaa#yahoo.com")
(1.3ms) BEGIN
User Exists (1.4ms) SELECT 1 FROM "users" WHERE ("users"."email" = 'aaaa#yahoo.com' AND "users"."id" != 1) LIMIT 1
(1.4ms) UPDATE "users" SET "unconfirmed_email" = 'aaaa#yahoo.com', "updated_at" = '2012-08-27 05:05:26.337961' WHERE "users"."id" = 1
User Load (1.4ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'Yyg98zno81adJt4mp7pG' LIMIT 1
(1.4ms) UPDATE "users" SET "unconfirmed_email" = 'aaaa#yahoo.com', "updated_at" = '2012-08-27 05:05:26.337961', "confirmation_token" = 'Yyg98zno81adJt4mp7pG', "confirmation_sent_at" = '2012-08-27 05:05:26.342299' WHERE "users"."id" = 1
Profile Load (1.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 1 LIMIT 1
(1.7ms) COMMIT
=> true
Any help would be much appreciated!
Thanks,
I was trying to figure out what the differences are between the two environments - dev/staging vs. production. Since staging and production have pretty much identical settings, the only I could think of was cacheing. So, I changed the update_attributes line to the following and it worked.
if #user.reload.update_attributes(:email => params[:email])

Rails: How to step-by-step debug a request

I have an ajax request that is causing problems in my Rails 3.0.9 app. I can see the problem in the logs, but I don't have any idea what is triggering it between the ajax call and the render. Here's the log, and the event I don't want with ** beside it:
Started DELETE "/notifications/13" for 127.0.0.1 at 2011-06-21 22:08:39 -0500
Processing by NotificationsController#destroy as JS
Parameters: {"id"=>"13"}
SQL (0.4ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
SQL (0.3ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Slug Load (0.4ms) SELECT "slugs".* FROM "slugs" WHERE ("slugs".sluggable_id = 1 AND "slugs".sluggable_type = 'User') ORDER BY id DESC LIMIT 1
****AREL (0.3ms) UPDATE "users" SET "remember_token" = NULL, "remember_created_at" = NULL, "updated_at" = '2011-06-22 03:08:40.084049', "preferences" = '---
:email_notifications: ''true''
' WHERE "users"."id" = 1
Notification Load (0.2ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."id" = 13 LIMIT 1
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
AREL (0.3ms) UPDATE "users" SET "notifications_count" = COALESCE("notifications_count", 0) - 1 WHERE "users"."id" = 1
AREL (0.1ms) DELETE FROM "notifications" WHERE "notifications"."id" = 13
Completed 200 OK in 1334ms
I'd like to somehow step by step debug this request, sort of like the way you can step through a function in javascript using firebug.
Is there a way to debug like this so I can see how that specific AREL command is getting called??
Have you looked at ruby on rails guides - debugging?? you can debug just like in gdb
This railscast is also quite useful.

Resources