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
Related
I have a table in below columns
id, user_id, friend_user_id
If A is a friend of B then, I want to insert two records such as
1. user_id: 1, friend_user_id: 2
2. user_id: 2, friend_user_id: 1
I've did this using the after_create callback as below
after_create do
Friend.create(user_id: friend_user_id,friend_user_id: user_id)
end
I want to delete both records, if any one of the record has been deleted.
I've tried the after_destroy callback as below.
after_destroy do
Friend.where(friend_user_id: user_id,user_id: friend_user_id).first.destroy
end
But I'm getting the below error.
2.3.0 :002 > Friend.first.destroy
Friend Load (0.4ms) SELECT "friends".* FROM "friends" ORDER BY "friends"."id" ASC LIMIT 1
(0.2ms) begin transaction
SQL (0.7ms) DELETE FROM "friends" WHERE "friends"."id" = ? [["id", 10]]
Friend Load (0.3ms) SELECT "friends".* FROM "friends" WHERE "friends"."friend_user_id" = ? AND "friends"."user_id" = ? ORDER BY "friends"."id" ASC LIMIT 1 [["friend_user_id", 1], ["user_id", 2]]
SQL (0.1ms) DELETE FROM "friends" WHERE "friends"."id" = ? [["id", 11]]
Friend Load (0.1ms) SELECT "friends".* FROM "friends" WHERE "friends"."friend_user_id" = ? AND "friends"."user_id" = ? ORDER BY "friends"."id" ASC LIMIT 1 [["friend_user_id", 2], ["user_id", 1]]
(0.3ms) rollback transaction
NoMethodError: undefined method `destroy' for nil:NilClass
from /home/ubuntu/workspace/app/models/friend.rb:39:in `block in <class:Friend>'
I'm new to RoR. Any help would be appreciated.
Your code is quite wrong here, you will end up getting SystemStackError and that happens in both the places in after_create and after_destroy. What is happening is that when you destroy an element, then your after_destroy callback is executed which will again destroy an element and since it has deleted an element it will again execute a callback, but since you have deleted that element that is why you are getting this error NoMethodError: undefined methoddestroy' for nil:NilClass` because that element has already been deleted, to get a more clear picture use the same code and do a Friend.create and you will see what I am saying.
Hello friends I want to ask them how to make rails console
does not show me the sqlite consult
Loading development environment (Rails 4.0.5)
2.0.0-p481 :001 > last_user=User.last
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 5, name: "Juan", email: "Lopez", created_at: "2014-06-02 19:50:48", updated_at: "2014-06-02 19:50:48">
I do not want to show this
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
In console type:
ActiveRecord::Base.logger = nil
I am having trouble returning all profiles that have blocked you with the amistad gem.
I am able to find profiles you have blocked.
And the profiles other individual profiles have blocked.
a = Profile.find_by_id 6
b = Profile.find_by_id 7
b.block_profile a
a.blockades
Profile Load (0.2ms) SELECT ".."
=> #<ActiveRecord::Associations::CollectionProxy [#<Profile id: 18....>]>
a.blockades_by
Profile Load (0.2ms) SELECT "profiles".* FROM "profiles" INNER JOIN "friendships" ON "profiles"."id" = "friendships"."friendable_id" WHERE "friendships"."blocker_id" = ? AND (friendable_id <> blocker_id) [["blocker_id", 6]]
=> #<ActiveRecord::Associations::CollectionProxy []>
b.blockades
Profile Load (0.2ms) SELECT "...."
=> #<ActiveRecord::Associations::CollectionProxy [#<Profile id: 6,....>]>
Oky, so it works if you know which profile exactly to look at.
But the issue is I would need to iterate through every profile and look at their blockades_by to see if the logged in user is there, then remove them from the final query. This is way too crazy. There needs to be a simpler way?
So I have a question model that belongs to User. Initially, I'd like to set basic questions with an attribute public: true, and every user can see this kind of questions which I'd like to create in the seed.rb.
Then, subsequently with an Answer model. Each users answer belongs to a certain question.
Now the issue is creating these pre-made questions for the users to answer I've tried the following in seed.rb:
u = User.new(email: "test#gmail.com", password: "testpass", password_confirmation: "testpass", gender: "M")
questions = u.questions.build(title: "What is your favourite food?")
u.save
And I'd like to call the same default questions for all users in the view with
questions_controller.rb
def index
#questions = Question.all
end
But this simply doesn't seem to be working, i.e. when I go to rails c and run u, it's an undefined method.. and the u.questions is an empty array. I've run these lines in console manually and they worked so I'm not sure what's happening here.
What am I doing wrong?
Update dev log
^[[1m^[[36mUser Load (16.9ms)^[[0m ^[[1mSELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1^[[0m
^[[1m^[[35mQuestion Load (645.9ms)^[[0m SELECT "questions".* FROM "questions" WHERE "questions"."user_id" = $1 [["user_id", 2]]
^[[1m^[[36mActiveRecord::SchemaMigration Load (48.4ms)^[[0m ^[[1mSELECT "schema_migrations".* FROM "schema_migrations"^[[0m
^[[1m^[[36mActiveRecord::SchemaMigration Load (1.0ms)^[[0m ^[[1mSELECT "schema_migrations".* FROM "schema_migrations"^[[0m
^[[1m^[[35m (0.3ms)^[[0m BEGIN
^[[1m^[[36mUser Exists (34.7ms)^[[0m ^[[1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'anthony#gmail.com' LIMIT 1^[[0m
^[[1m^[[35m (0.3ms)^[[0m ROLLBACK
DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from service a\
t /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138)
I think the issue was that the OP had bad data in the development database.
Running rake db:drop db:create db:setup appeared to fix the issue.
[1] pry(main)> User.first
User Load (0.4ms) SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 3, email: "chasity#kiehn.net", encrypted_password: "$2a$10$lqsgKvQuz9iSIu/..FMRJu76H9KNhfVz5x9DmxphC0TK...", reset_password_token: ... >
[2] pry(main)> User.find(1)
User Load (12.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
=> #<User id: 1, email: "admin#example.com", encrypted_password: "$2a$10$pGDcv0/EgiDk4KcZN9pli.evx5Ni1qOoujQD15HgWH8Y...", reset_password_token: ... >
[3] pry(main)> Product.first
Product Load (0.6ms) SELECT "products".* FROM "products" LIMIT 1
=> #<Product id: 1, name: "Ruby on Rails Jr. Spaghetti", created_at: "2012-01-25 10:13:26", updated_at: "2012-01-25 10:13:26", properties: ...>
This is happening in the console and during runtime and only for the User model. What could cause such behaviour?
You haven't specified an order - the database is free to order the results in any way it wants. It could be primary key order, it could be how things are laid out on disc but without an order clause there are no guarantees.
If you want the first record in a specific order, then make sure you ask for it
User.first is the same as User.all.first, there is no specified order, so the DB returns the list of element in any order. If you want to get the User with smallest id, use User.order("id").first.
If your request returns several elements and you want to pick any of them, you may want to use first without specified order.