Currently I am facing a problem in which I am getting the Timeout::Error. Here is my detailed scenario
I have following tables
- clients
- users - (contains client_id)
- scheme_transactions - (contains user_id)
Consider an example, I have a client (stored in the clients table) say ABCL which has around 1000 users (stored in the users table) and there are around 50000 transactions (stored in the scheme_transactions table) done by all of the respective client users.
Now I have to find total of the first transaction of each user. For this I have written following logic and it is working fine on the rails console but getting Time out error in the browser.
users = User.joins(:client).where(clients: {id: clients})
first_purchase = Array.new
users.each do |user|
user_transaction = SchemeTransaction.joins(:user).where(scheme_transactions: {user_id: user.id}).first
first_purchase << user_transaction.points unless user_transaction.nil?
end
total_first_purchase = first_purchase.inject(:+)
So is there any other way to avoid the problem or how can I simplify my logic.
I am using ruby 1.9.3 and rails 3.2.11
Aucun commentaire:
Enregistrer un commentaire