I would like to ask advice in my little problem.
This is my class, this is my controller.
I want to make this application multithreaded. Tell me please, which path would be best?
The controller's fast hack does not solve the problem:
if params[:select].present?
threads = []
params[:select].each do |item|
threads << Thread.new {
tweet = current_user.tweet.detect {
|t| item == t.name
}
config = {
.....
etc
}
end
threads.each(&:join)
It does not work, the process stops immediately:
Started GET "/tweets?select%5B%5D=adamasmit&select_action=follow&tag=&tag1=" for 127.0.0.1 at 2019-03-06 01:37:28 +0300
Processing by TweetsController#index as */*
Parameters: {"select"=>["adamasmit"], "select_action"=>"follow", "tag"=>"", "tag1"=>""}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 11], ["LIMIT", 1]]
Tweet Load (0.6ms) SELECT "tweets".* FROM "tweets" WHERE "tweets"."user_id" = ? [["user_id", 11]]
and that is all.
This is an example of the normal operation of the application:
Started GET "/tweets?select=adamasmit&select_action=unfollow&tag=&tag1=" for 127.0.0.1 at 2019-03-06 18:17:43 +0300
Processing by TweetsController#index as */*
Parameters: {"select"=>"adamasmit", "select_action"=>"unfollow", "tag"=>"", "tag1"=>""}
User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 11], ["LIMIT", 1]]
Tweet Load (0.3ms) SELECT "tweets".* FROM "tweets" WHERE "tweets"."user_id" = ? [["user_id", 11]]
adding follower to an array: cmirnow
adding follower to an array: travel_slovenia
adding follower to an array: godraksha
.....
etc
adding follower to an array: Pkakooza
adding follower to an array: chrissycrew3
adding friend to an array: cmirnow
adding friend to an array: travel_slovenia
adding friend to an array: godraksha
.....
etc
adding friend to an array: Pkakooza
adding friend to an array: chrissycrew3
adding friend to an array: RivaresF
follow: RivaresF
Rendering tweets/index.html.erb within layouts/application
Rendered tweets/index.html.erb within layouts/application (3.7ms)
Completed 200 OK in 3311ms (Views: 132.1ms | ActiveRecord: 2.6ms)
(0.1ms) begin transaction
(0.1ms) commit transaction
What do you advise?
the rails way would be to use ActiveJob to perform such expensive operations in the background: https://guides.rubyonrails.org/active_job_basics.html
Related
I am creating a blog and I want to show profile containing all posts of a perticular user by clicking the "uploader" link in index.html.erb(line no. 9). I used a controller named Pages and defined profile in it and linked it to "uploader" and passed user of that post.
code screenshot
I am getting error "Couldn't find User with 'id'="
error screenshot
terminal is showing User id as nil
Started GET "/" for 127.0.0.1 at 2017-03-27 02:08:49 +0530
Processing by PostsController#index as HTML
Post Load (0.5ms) SELECT "posts".* FROM "posts" ORDER BY created_at DESC
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 3]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Rendered posts/index.html.erb within layouts/application (10.8ms)
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]]
Completed 200 OK in 84ms (Views: 81.4ms | ActiveRecord: 1.1ms)
Started GET "/pages/profile.3" for 127.0.0.1 at 2017-03-27 01:49:02 +0530
Processing by PagesController#profile as
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 1ms (ActiveRecord: 0.1ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
app/controllers/pages_controller.rb:3:in `profile'
What am I doing wrong?
Any better method to do this?
Your problem is the route. This:
GET "/pages/profile.3"
should really be this:
GET "/pages/profile/3"
and that's caused by your route missing the required parameter. Change it to
# routes.rb
get 'pages/profile/:id
and it should work.
So I have a model that has an enum value like so:
class Connection < ActiveRecord::Base
enum request_status: { pending: 0, accepted: 1, rejected: 2, removed: 3 }
end
But, I have a params value that I want to set and do a where query on - something like this:
#connections = current_user.all_connections.where(request_status: params[:request_status])
current_user.all_connections returns an AR object (aka...it is also a query method with a where call).
The issue I am facing is that right now, the above query returns an empty collection. The reason I believe is that per the API docs in Rails:
Where conditions on an enum attribute must use the ordinal value of an
enum.
Given that params[:request_status] will look like this:
Parameters: {"request_status"=>"accepted"}
How would I modify that where query to reflect this? I know one other option is just to modify the parameter to be the ordinal value, rather than the string, but I am curious how I might achieve this with the string value rather than the ordinal value?
Edit 1
Here are the server logs:
Started GET "/connections?request_status=accepted" for 127.0.0.1 at 2016-01-04 20:23:08 -0500
Processing by ConnectionsController#index as HTML
Parameters: {"request_status"=>"accepted"}
User Load (2.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (1.7ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 3]]
Connection Load (2.0ms) SELECT "connections".* FROM "connections" WHERE "connections"."invited_user_id" = $1 ORDER BY "connections"."created_at" DESC [["invited_user_id", 3]]
Connection Load (1.8ms) SELECT "connections".* FROM "connections" WHERE "connections"."inviter_user_id" = $1 ORDER BY "connections"."created_at" DESC [["inviter_user_id", 3]]
Connection Load (2.2ms) SELECT "connections".* FROM "connections" WHERE (connections.invited_user_id = 3 OR connections.inviter_user_id = 3) AND "connections"."request_status" = 0 ORDER BY "connections"."created_at" DESC
Rendered connections/index.html.erb within layouts/connections (0.3ms)
Rendered connections/_header.html.erb (2.8ms)
Rendered shared/_navbar.html.erb (61.2ms)
Rendered shared/_footer.html.erb (3.2ms)
Rendered layouts/application.html.erb (1142.4ms)
Completed 200 OK in 1184ms (Views: 1155.5ms | ActiveRecord: 10.4ms)
Edit 2
This is the index action for that controller.
def index
params[:request_status] = "pending" if params[:request_status].nil?
#connections = current_user.all_connections.where(request_status: params[:request_status]).order(created_at: :desc).group_by { |c| c.created_at.to_date }
end
What's strange is that for every action that I click on that should send the correct request_status it still sends the SQL with request_status = 0.
I commented out the params[:request_status] = setter in the controller to see if that was doing it, but it wasn't it.
What could be causing this issue?
Here is another log for a different status:
Started GET "/connections?request_status=rejected" for 127.0.0.1 at 2016-01-04 21:30:29 -0500
Processing by ConnectionsController#index as HTML
Parameters: {"request_status"=>"rejected"}
User Load (2.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (1.5ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 3]]
Connection Load (1.4ms) SELECT "connections".* FROM "connections" WHERE "connections"."invited_user_id" = $1 ORDER BY "connections"."created_at" DESC [["invited_user_id", 3]]
Connection Load (1.0ms) SELECT "connections".* FROM "connections" WHERE "connections"."inviter_user_id" = $1 ORDER BY "connections"."created_at" DESC [["inviter_user_id", 3]]
Connection Load (1.2ms) SELECT "connections".* FROM "connections" WHERE (connections.invited_user_id = 3 OR connections.inviter_user_id = 3) AND "connections"."request_status" = 0 ORDER BY "connections"."created_at" DESC
Rendered connections/index.html.erb within layouts/connections (0.2ms)
Rendered connections/_header.html.erb (2.5ms)
Rendered shared/_navbar.html.erb (60.3ms)
Rendered shared/_footer.html.erb (3.0ms)
Rendered layouts/application.html.erb (1103.8ms)
Completed 200 OK in 1130ms (Views: 1112.5ms | ActiveRecord: 7.3ms)
You can use Connection.request_statuses["pending"] to get 0.
So you can use string in your query like this:
#connections = current_user.all_connections.where(request_status: Connection.request_statuses[params[:request_status]])
I'm trying to update the shop_id of a user when they click an add button.
The controller method called is this:
def update_shop
#user = User.find(params[:id])
#shop = Shop.find(params[:shop_id])
#user.update_attribute(:shop_id, params[:shop_id])
flash[:success] = "Added Shop!"
redirect_to #shop
end
The server when the button is clicked reads:
Started POST "/updateshop?id=3&shop_id=1" for ::1 at 2015-08-31 05:50:52 -0500
Processing by UsersController#update_shop as HTML
Parameters: {"authenticity_token"=>"jRozldw1u3TrWhaL6CeJyw4Tm5V5S/IFEQQulRkuV1Ot85kmPOsMa2jH2L6m8EFDpy7Ygc9SMBvPLJCuosHXUg==", "id"=>"3", "shop_id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 3]]
Shop Load (0.1ms) SELECT "shops".* FROM "shops" WHERE "shops"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.3ms) UPDATE "users" SET "shop_id" = ?, "updated_at" = ? WHERE "users"."id" = ? [["shop_id", 1], ["updated_at", "2015-08-31 10:50:52.089826"], ["id", 3]]
(5.8ms) commit transaction
But it doesn't actually update User.shop_id
And sometimes it doesn't have the update line, and reads:
Started POST "/updateshop?id=3&shop_id=1" for ::1 at 2015-08-31 05:56:54 -0500
Processing by UsersController#update_shop as HTML
Parameters: {"authenticity_token"=>"D1ODlfDnhJmQ9NUfh+GL2JE747nJC2t4eqOziRGNCaUvuikmEDkzhhNpGyrJNkNQOAagrX8SqWakiw2yqmKJpA==", "id"=>"3", "shop_id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 3]]
Shop Load (0.1ms) SELECT "shops".* FROM "shops" WHERE "shops"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
(0.1ms) commit transaction
The parameters are passed correctly and I think update_attribute is correct, what's going wrong?
The value is already assigned, this is why it's not updated. I mean, there is no actual changes, this is why no UPDATE statements were issued.
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.
I have the following:
class User < ActiveRecord::Base
before_update :guest_upgrade
def guest_upgrade
# If the user changed their email that means they were a guest, and are no longer.
# Likely triggered from the Registrations#Update controller
if self.email_changed?
self.guest = false
end
end
This is causing rollbacks, here is the log with the above in play:
Started POST "/users" for 127.0.0.1 at 2011-07-18 14:54:00 -0700
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x+u1DSDanU2QXK/q0=", "user"=>{"fname"=>"xxxxx", "lname"=>"xxxx", "email"=>"xxxxxxx#gmail.com", "password"=>"[FILTERED]", "remember_me"=>"1"}, "commit"=>"Create my account", "fb_access_token"=>"", "fb_uuid"=>""}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
SQL (0.1ms) BEGIN
User Load (0.5ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'xxxxxxx#gmail.com') AND ("users".id <> 5) LIMIT 1
SQL (0.2ms) ROLLBACK
Authentication Load (0.5ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND ("authentications".user_id = 5) LIMIT 1
Rendered layouts/_header.html.erb (3.9ms)
CACHE (0.0ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND ("authentications".user_id = 5) LIMIT 1
Rendered registrations/edit.html.erb within layouts/application (107.0ms)
Completed 200 OK in 484ms (Views: 111.7ms | ActiveRecord: 6.0ms)
Yet if I comment out the guest_upgrade it works fine:
Started POST "/users" for 127.0.0.1 at 2011-07-18 14:55:23 -0700
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxx+u1DSDanU2QXK/q0=", "user"=>{"fname"=>"XXXX", "lname"=>"XXXX", "email"=>"xxxx#gmail.com"}, "commit"=>"Save Changes"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
SQL (0.1ms) BEGIN
User Load (0.4ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'xxxx#gmail.com') AND ("users".id <> 5) LIMIT 1
AREL (0.7ms) UPDATE "users" SET "fname" = 'XXXX', "lname" = 'XXXX', "email" = 'xxxx#gmail.com', "updated_at" = '2011-07-18 21:55:23.817142' WHERE "users"."id" = 5
[paperclip] Saving attachments.
SQL (37.9ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 780ms
Am I using the dirty objects incorrectly?
All I want to do is, when User is updated, if the user changes there email, change the user.guest field to false.
Suggestions? Thanks
I think I've had this problem before. I believe what's happening is it's returning false; try adding an explicit return like so:
if self.email_changed?
self.guest = false
return true
end
Check out the section marked Canceling Callbacks at ruby on rails.org api. It reads:
Canceling callbacks
If a before_* callback returns false, all the later callbacks and the
associated action are cancelled. If an after_* callback returns false,
all the later callbacks are cancelled. Callbacks are generally run in
the order they are defined, with the exception of callbacks defined as
methods on the model, which are called last.
I know I'm late but I hope this helps!