I was given a legacy spree app and I am supposed to edit one of the deeper checkout pages, but I can't get to it on my local because it keeps redirecting me to the shopping cart page with the flash:
Can't check out, no payment methods are configured for this environment
My database should be identical to the production database. I can't find Can't check out, no payment methods are configured for this environment anywhere in the project. How would I get past this?
I checked the log and it looks like there's a before_filter that's preventing me from going through: Filter chain halted as [:ensure_payment_methods] rendered_or_redirected.. However, I can't find this method anywhere in the code. How do I edit controllers that don't exist in my project? How do I find out where they are?
Here's my log:
Processing CheckoutsController#edit (for 127.0.0.1 at 2013-09-20 10:26:15) [GET]
Parameters: {"action"=>"edit", "controller"=>"checkouts", "order_id"=>"R838445544"}
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Order Load (0.6ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Cache hit: Spree::Config ({})
Zone Load (0.3ms) SELECT * FROM "zones" WHERE ("zones"."name" IS NULL) LIMIT 1
Country Load (0.8ms) SELECT * FROM "countries"
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Checkout Load (0.7ms) SELECT * FROM "checkouts" WHERE ("checkouts".order_id = 4744) LIMIT 1
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Cache hit: Spree::Config ({})
Country Load (0.2ms) SELECT * FROM "countries" WHERE ("countries"."id" = 214)
Cache hit: Spree::Config ({})
CACHE (0.0ms) SELECT * FROM "countries" WHERE ("countries"."id" = 214)
State Load (0.4ms) SELECT * FROM "states" WHERE ("states".country_id = 214)
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Redirected to http://localhost:3000/boutique/orders/R838445544/checkout/register
Filter chain halted as [:enforce_registration] rendered_or_redirected.
Completed in 77ms (DB: 5) | 302 Found [http://localhost/boutique/orders/R838445544/checkout/edit]
Property Load (0.3ms) SELECT * FROM "properties" WHERE ("properties"."name" = 'brand') LIMIT 1
AppConfiguration Load (0.4ms) SELECT * FROM "configurations" WHERE ("configurations"."name" = 'Default configuration') AND ( ("configurations"."type" = 'AppConfiguration' ) ) LIMIT 1
Cache hit: Spree::Config ({})
requiring dependency
finished requiring.
SQL (0.1ms) SET client_min_messages TO 'panic'
SQL (0.1ms) SET standard_conforming_strings = on
SQL (0.1ms) SET client_min_messages TO 'notice'
User Load IDs For Limited Eager Loading (1.3ms) SELECT DISTINCT "users".id FROM "users" LEFT OUTER JOIN "roles_users" ON "roles_users".user_id = "users".id LEFT OUTER JOIN "roles" ON "roles".id = "roles_users".role_id WHERE (roles.name = 'admin') LIMIT 1
User Load Including Associations (0.8ms) SELECT "users"."id" AS t0_r0, "users"."email" AS t0_r1, "users"."crypted_password" AS t0_r2, "users"."salt" AS t0_r3, "users"."remember_token" AS t0_r4, "users"."remember_token_expires_at" AS t0_r5, "users"."created_at" AS t0_r6, "users"."updated_at" AS t0_r7, "users"."persistence_token" AS t0_r8, "users"."single_access_token" AS t0_r9, "users"."perishable_token" AS t0_r10, "users"."login_count" AS t0_r11, "users"."failed_login_count" AS t0_r12, "users"."last_request_at" AS t0_r13, "users"."current_login_at" AS t0_r14, "users"."last_login_at" AS t0_r15, "users"."current_login_ip" AS t0_r16, "users"."last_login_ip" AS t0_r17, "users"."login" AS t0_r18, "users"."ship_address_id" AS t0_r19, "users"."bill_address_id" AS t0_r20, "users"."openid_identifier" AS t0_r21, "users"."api_key" AS t0_r22, "users"."creditcard_id" AS t0_r23, "roles"."id" AS t1_r0, "roles"."name" AS t1_r1 FROM "users" LEFT OUTER JOIN "roles_users" ON "roles_users".user_id = "users".id LEFT OUTER JOIN "roles" ON "roles".id = "roles_users".role_id WHERE (roles.name = 'admin') AND "users".id IN (1)
Processing CheckoutsController#register (for 127.0.0.1 at 2013-09-20 10:26:17) [GET]
Parameters: {"action"=>"register", "controller"=>"checkouts", "order_id"=>"R838445544"}
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Order Load (0.5ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Cache hit: Spree::Config ({})
Zone Load (0.2ms) SELECT * FROM "zones" WHERE ("zones"."name" IS NULL) LIMIT 1
Country Load (0.7ms) SELECT * FROM "countries"
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Checkout Load (0.6ms) SELECT * FROM "checkouts" WHERE ("checkouts".order_id = 4744) LIMIT 1
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Cache hit: Spree::Config ({})
Country Load (0.2ms) SELECT * FROM "countries" WHERE ("countries"."id" = 214)
Cache hit: Spree::Config ({})
CACHE (0.0ms) SELECT * FROM "countries" WHERE ("countries"."id" = 214)
State Load (0.4ms) SELECT * FROM "states" WHERE ("states".country_id = 214)
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
PaymentMethod Load (0.4ms) SELECT * FROM "payment_methods" WHERE ("payment_methods"."deleted_at" IS NULL)
Redirected to http://localhost:3000/boutique/orders/R838445544/edit
Filter chain halted as [:ensure_payment_methods] rendered_or_redirected.
Completed in 83ms (DB: 5) | 302 Found [http://localhost/boutique/orders/R838445544/checkout/register]
Property Load (0.3ms) SELECT * FROM "properties" WHERE ("properties"."name" = 'brand') LIMIT 1
AppConfiguration Load (0.3ms) SELECT * FROM "configurations" WHERE ("configurations"."name" = 'Default configuration') AND ( ("configurations"."type" = 'AppConfiguration' ) ) LIMIT 1
Cache hit: Spree::Config ({})
requiring dependency
finished requiring.
SQL (0.1ms) SET client_min_messages TO 'panic'
SQL (0.1ms) SET standard_conforming_strings = on
SQL (0.1ms) SET client_min_messages TO 'notice'
User Load IDs For Limited Eager Loading (1.3ms) SELECT DISTINCT "users".id FROM "users" LEFT OUTER JOIN "roles_users" ON "roles_users".user_id = "users".id LEFT OUTER JOIN "roles" ON "roles".id = "roles_users".role_id WHERE (roles.name = 'admin') LIMIT 1
User Load Including Associations (0.8ms) SELECT "users"."id" AS t0_r0, "users"."email" AS t0_r1, "users"."crypted_password" AS t0_r2, "users"."salt" AS t0_r3, "users"."remember_token" AS t0_r4, "users"."remember_token_expires_at" AS t0_r5, "users"."created_at" AS t0_r6, "users"."updated_at" AS t0_r7, "users"."persistence_token" AS t0_r8, "users"."single_access_token" AS t0_r9, "users"."perishable_token" AS t0_r10, "users"."login_count" AS t0_r11, "users"."failed_login_count" AS t0_r12, "users"."last_request_at" AS t0_r13, "users"."current_login_at" AS t0_r14, "users"."last_login_at" AS t0_r15, "users"."current_login_ip" AS t0_r16, "users"."last_login_ip" AS t0_r17, "users"."login" AS t0_r18, "users"."ship_address_id" AS t0_r19, "users"."bill_address_id" AS t0_r20, "users"."openid_identifier" AS t0_r21, "users"."api_key" AS t0_r22, "users"."creditcard_id" AS t0_r23, "roles"."id" AS t1_r0, "roles"."name" AS t1_r1 FROM "users" LEFT OUTER JOIN "roles_users" ON "roles_users".user_id = "users".id LEFT OUTER JOIN "roles" ON "roles".id = "roles_users".role_id WHERE (roles.name = 'admin') AND "users".id IN (1)
Processing OrdersController#edit (for 127.0.0.1 at 2013-09-20 10:26:19) [GET]
Parameters: {"action"=>"edit", "controller"=>"orders", "id"=>"R838445544"}
Order Load (0.5ms) SELECT * FROM "orders" WHERE ("orders"."number" = 'R838445544') LIMIT 1
Adjustment Load (0.4ms) SELECT "adjustments".* FROM "adjustments" WHERE ("adjustments".order_id = 4744) ORDER BY position
Cache hit: Spree::Config ({})
Checkout Load (0.6ms) SELECT * FROM "checkouts" WHERE ("checkouts".order_id = 4744) LIMIT 1
SQL (0.1ms) BEGIN
Order Load (0.2ms) SELECT * FROM "orders" WHERE ("orders"."id" = 4744)
Shipment Load (1.8ms) SELECT * FROM "shipments" WHERE ("shipments".order_id = 4744) ORDER BY shipments.id DESC LIMIT 1
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."id" = 4744)
SQL (0.1ms) COMMIT
SQL (0.1ms) BEGIN
LineItem Load (0.4ms) SELECT * FROM "line_items" WHERE ("line_items".order_id = 4744)
Variant Load (0.5ms) SELECT * FROM "variants" WHERE ("variants"."id" = 179)
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."id" = 4744)
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
InventoryUnit Load (0.5ms) SELECT * FROM "inventory_units" WHERE (inventory_units.order_id = 4744) LIMIT 1
Cache hit: Spree::Config ({})
Shipment Load (1.0ms) SELECT * FROM "shipments" WHERE ("shipments".order_id = 4744)
Charge Load (0.3ms) SELECT * FROM "adjustments" WHERE ("adjustments".order_id = 4744) AND ( ("adjustments"."type" = 'Charge' OR "adjustments"."type" = 'TaxCharge' OR "adjustments"."type" = 'ShippingCharge' ) ) ORDER BY position
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."id" = 4744)
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."id" = 4744)
CACHE (0.0ms) SELECT * FROM "shipments" WHERE ("shipments".order_id = 4744) ORDER BY shipments.id DESC LIMIT 1
Cache hit: Spree::Config ({})
Credit Load (0.2ms) SELECT * FROM "adjustments" WHERE ("adjustments".order_id = 4744) AND ( ("adjustments"."type" = 'Credit' OR "adjustments"."type" = 'GiftCardCredit' OR "adjustments"."type" = 'CouponCredit' OR "adjustments"."type" = 'ManagersDiscountCredit' OR "adjustments"."type" = 'ReturnAuthorizationCredit' ) ) ORDER BY position
SQL (0.1ms) COMMIT
Rendering template within layouts/spree_application
Rendering orders/edit
Image Load (0.6ms) SELECT * FROM "assets" WHERE ("assets".viewable_id = 179 AND "assets".viewable_type = 'Variant') AND ( ("assets"."type" = 'Image' OR "assets"."type" = 'ProductThumbnail' OR "assets"."type" = 'ProductPageImage' ) ) ORDER BY position
Product Load (0.7ms) SELECT * FROM "products" WHERE ("products"."id" = 85)
SQL (0.5ms) SELECT count(*) AS count_all FROM "assets" WHERE ("assets".viewable_id = 85 AND "assets".viewable_type = 'Product') AND ( ("assets"."type" = 'Image' OR "assets"."type" = 'ProductThumbnail' OR "assets"."type" = 'ProductPageImage' ) )
Image Load (0.3ms) SELECT * FROM "assets" WHERE ("assets".viewable_id = 85 AND "assets".viewable_type = 'Product') AND ( ("assets"."type" = 'Image' OR "assets"."type" = 'ProductThumbnail' OR "assets"."type" = 'ProductPageImage' ) ) ORDER BY position LIMIT 1
Cache hit: Spree::Config ({})
OptionValue Load (0.7ms) SELECT * FROM "option_values" INNER JOIN "option_values_variants" ON "option_values".id = "option_values_variants".option_value_id WHERE ("option_values_variants".variant_id = 179 )
OptionType Load (0.3ms) SELECT * FROM "option_types" WHERE ("option_types"."id" = 1)
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Rendered orders/_line_item (195.9ms)
Cache hit: Spree::Config ({})
Cache hit: Spree::Config ({})
Rendered orders/_form (201.2ms)
Rendered orders/_delivery (0.9ms)
Taxonomy Load (0.6ms) SELECT * FROM "taxonomies" WHERE (name != 'Featured Products') ORDER BY id
Taxon Load (0.7ms) SELECT "taxons".* FROM "taxons" WHERE ("taxons".taxonomy_id IN (1,5,6,7,8) AND (parent_id is null))
Taxon Load (0.3ms) SELECT "taxons".* FROM "taxons" WHERE ("taxons".parent_id IN (1,33,34,35,37)) ORDER BY "lft"
Rendered shared/_taxonomies (25.5ms)
Cache hit: Spree::Config ({})
Rendered shared/_head (2.7ms)
Rendered shared/_store_menu (0.7ms)
Order Load (0.3ms) SELECT * FROM "orders" WHERE ("orders"."id" = 4744) LIMIT 1
CACHE (0.0ms) SELECT * FROM "orders" WHERE ("orders"."id" = 4744) LIMIT 1
CACHE (0.0ms) SELECT * FROM "line_items" WHERE ("line_items".order_id = 4744)
Rendered shared/_nav_bar (3.5ms)
Taxon Load (0.2ms) SELECT * FROM "taxons" WHERE ("taxons"."permalink" = 'caviar/') LIMIT 1
**#selected_tab is nil
Tracker Load (0.4ms) SELECT * FROM "trackers" WHERE ("trackers"."active" = 't' AND "trackers"."environment" = 'development') LIMIT 1
Rendered shared/_google_analytics (14.7ms)
Rendered shared/_footer (23.6ms)
Completed in 400ms (View: 295, DB: 15) | 200 OK [http://localhost/boutique/orders/R838445544/edit]
Property Load (0.3ms) SELECT * FROM "properties" WHERE ("properties"."name" = 'brand') LIMIT 1
AppConfiguration Load (0.4ms) SELECT * FROM "configurations" WHERE ("configurations"."name" = 'Default configuration') AND ( ("configurations"."type" = 'AppConfiguration' ) ) LIMIT 1
Cache hit: Spree::Config ({})
requiring dependency
finished requiring.
If you look at your spree_payment_methods table you will see a column called environment. Most likely you don't have any rows with the value of 'development' in you environment column.
Navigate to /admin
click configuration
in the right side list click on payment methods
Create a payment method and that should solve your problem.
In general you want to clone a local copy of Spree or whatever Spree expansions you have in your Gemfile, and grep on that to find what you need.
Related
my app is crashing randomly in development.
I start using Puma, sometimes on page request it crash.
No log, only a generic error 500.
Now I switch to Webrick, the same.
CACHE Product Load (0.0ms) SELECT "products".* FROM "products" INNER JOIN "product_relations" ON "products"."id" = "product_relations"."product_id" WHERE "product_relations"."productable_id" = $1 AND "product_relations"."productable_type" = $2 AND "products"."state" = $3 AND "products"."published_at" <= '2020-05-04 20:52:34.630525' ORDER BY "products"."published_at" DESC LIMIT $4 [["productable_id", 34969], ["productable_type", "Content"], ["state", "active"], ["LIMIT", 11]]
CACHE Brand Load (0.0ms) SELECT "brands".* FROM "brands" INNER JOIN "content_brands" ON "brands"."id" = "content_brands"."brand_id" WHERE "content_brands"."content_id" = $1 ORDER BY "brands"."name" ASC LIMIT $2 [["content_id", 34969], ["LIMIT", 11]]
CACHE Product Load (0.0ms) SELECT "products".* FROM "products" INNER JOIN "product_relations" ON "products"."id" = "product_relations"."product_id" WHERE "product_relations"."productable_id" = $1 AND "product_relations"."productable_type" = $2 AND "products"."state" = $3 AND "products"."published_at" <= '2020-05-04 20:52:34.630525' ORDER BY "products"."published_at" DESC LIMIT $4 [["productable_id", 34969], ["productable_type", "Content"], ["state", "active"], ["LIMIT", 11]]
CACHE Brand Load (0.0ms) SELECT "brands".* FROM "brands" INNER JOIN "content_brands" ON "brands"."id" = "content_brands"."brand_id" WHERE "content_brands"."content_id" = $1 ORDER BY "brands"."name" ASC LIMIT $2 [["content_id", 34969], ["LIMIT", 11]]
Rendered app/cells/product_reviews/show.html.haml (Duration: 77.6ms | Allocations: 31933)
Rendered contents/review.html.haml within layouts/application (Duration: 184.7ms | Allocations: 109291)
Completed 500 Internal Server Error in 233ms (ActiveRecord: 23.7ms | Allocations: 140545)
I need to reboot the server, and then the page load.
No error, only a generic Completed 500 Internal Server Error
I have in my development.rb
config.consider_all_requests_local = true
and
config.log_level = :debug
Is there a deeper log???
My Rails app generates double requests with every page load, and I'm not able to diagnose the source of the problem. Answers here suggest removing all blank/self-referencing hrefs and running rake assets:clean - but neither has any impact.
Both of these requests are identical, save for timestamps and the as part of the controller action processing. The first request says Processing by TradesController#index as HTML whereas the second says Processing by TradesController#index as */*
Has anyone else dealt with this, and if so, how did you fix it?
Started GET "/swaps" for 2600:1700:ba01:ff10:cc31:d814:a204:d70e at 2020-02-23 14:32:25 -0800
Processing by TradesController#index as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.4ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_requester_id" = $1 AND "trades"."approved_at" IS NULL [["trade_requester_id", 1]]
(0.3ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Rendering trades/index.html.erb within layouts/application
Rendered trades/_trades_menu.html.erb (0.5ms)
CACHE (0.0ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Rendered trades/index.html.erb within layouts/application (3.0ms)
CACHE (0.0ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Transaction Load (0.5ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."recipient_id" = $1 AND "transactions"."archived_by_recipient" = $2 [["recipient_id", 1], ["archived_by_recipient", false]]
Transaction Load (0.5ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."sender_id" = $1 AND (state = 'approved' OR state = 'force_flipped' OR state = 'payment_sent' OR state = 'payment_declined' OR state = 'passed' OR state = 'paid' OR state = 'refunded' OR state = 'shipped' OR state = 'received' OR state = 'not_received' OR state = 'unpaid' OR state = 'ignored' OR state = 'declined') AND "transactions"."archived_by_sender" = $2 [["sender_id", 1], ["archived_by_sender", false]]
PaymentTransfer Load (0.3ms) SELECT "payment_transfers".* FROM "payment_transfers" WHERE "payment_transfers"."user_id" = $1 AND (user_id = 1 AND read = FALSE) [["user_id", 1]]
CACHE Transaction Load (0.0ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."recipient_id" = $1 AND "transactions"."archived_by_recipient" = $2 [["recipient_id", 1], ["archived_by_recipient", false]]
CACHE Transaction Load (0.0ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."sender_id" = $1 AND (state = 'approved' OR state = 'force_flipped' OR state = 'payment_sent' OR state = 'payment_declined' OR state = 'passed' OR state = 'paid' OR state = 'refunded' OR state = 'shipped' OR state = 'received' OR state = 'not_received' OR state = 'unpaid' OR state = 'ignored' OR state = 'declined') AND "transactions"."archived_by_sender" = $2 [["sender_id", 1], ["archived_by_sender", false]]
CACHE PaymentTransfer Load (0.0ms) SELECT "payment_transfers".* FROM "payment_transfers" WHERE "payment_transfers"."user_id" = $1 AND (user_id = 1 AND read = FALSE) [["user_id", 1]]
(1.2ms) SELECT COUNT(*) FROM "conversations" INNER JOIN "messages" ON "messages"."conversation_id" = "conversations"."id" INNER JOIN "users" ON "users"."id" = "messages"."user_id" WHERE ((conversations.sender_id = 1 OR conversations.receiver_id = 1)) AND (messages.user_id != 1 AND read = FALSE)
Rendered layouts/_header.html.erb (9.2ms)
Completed 200 OK in 58ms (Views: 48.5ms | ActiveRecord: 4.2ms)
(0.2ms) BEGIN
(0.2ms) COMMIT
Started GET "/swaps" for 2600:1700:ba01:ff10:cc31:d814:a204:d70e at 2020-02-23 14:32:26 -0800
Processing by TradesController#index as */*
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.4ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_requester_id" = $1 AND "trades"."approved_at" IS NULL [["trade_requester_id", 1]]
(0.4ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Rendering trades/index.html.erb within layouts/application
Rendered trades/_trades_menu.html.erb (0.6ms)
CACHE (0.0ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Rendered trades/index.html.erb within layouts/application (27.1ms)
CACHE (0.0ms) SELECT COUNT(*) FROM "trades" WHERE "trades"."trade_recipient_id" = $1 AND "trades"."approved_at" IS NULL [["trade_recipient_id", 1]]
Transaction Load (0.5ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."recipient_id" = $1 AND "transactions"."archived_by_recipient" = $2 [["recipient_id", 1], ["archived_by_recipient", false]]
Transaction Load (0.6ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."sender_id" = $1 AND (state = 'approved' OR state = 'force_flipped' OR state = 'payment_sent' OR state = 'payment_declined' OR state = 'passed' OR state = 'paid' OR state = 'refunded' OR state = 'shipped' OR state = 'received' OR state = 'not_received' OR state = 'unpaid' OR state = 'ignored' OR state = 'declined') AND "transactions"."archived_by_sender" = $2 [["sender_id", 1], ["archived_by_sender", false]]
PaymentTransfer Load (0.3ms) SELECT "payment_transfers".* FROM "payment_transfers" WHERE "payment_transfers"."user_id" = $1 AND (user_id = 1 AND read = FALSE) [["user_id", 1]]
CACHE Transaction Load (0.0ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."recipient_id" = $1 AND "transactions"."archived_by_recipient" = $2 [["recipient_id", 1], ["archived_by_recipient", false]]
CACHE Transaction Load (0.0ms) SELECT "transactions".* FROM "transactions" WHERE "transactions"."sender_id" = $1 AND (state = 'approved' OR state = 'force_flipped' OR state = 'payment_sent' OR state = 'payment_declined' OR state = 'passed' OR state = 'paid' OR state = 'refunded' OR state = 'shipped' OR state = 'received' OR state = 'not_received' OR state = 'unpaid' OR state = 'ignored' OR state = 'declined') AND "transactions"."archived_by_sender" = $2 [["sender_id", 1], ["archived_by_sender", false]]
CACHE PaymentTransfer Load (0.0ms) SELECT "payment_transfers".* FROM "payment_transfers" WHERE "payment_transfers"."user_id" = $1 AND (user_id = 1 AND read = FALSE) [["user_id", 1]]
(0.7ms) SELECT COUNT(*) FROM "conversations" INNER JOIN "messages" ON "messages"."conversation_id" = "conversations"."id" INNER JOIN "users" ON "users"."id" = "messages"."user_id" WHERE ((conversations.sender_id = 1 OR conversations.receiver_id = 1)) AND (messages.user_id != 1 AND read = FALSE)
Rendered layouts/_header.html.erb (8.7ms)
Completed 200 OK in 152ms (Views: 116.6ms | ActiveRecord: 4.1ms)
(0.1ms) BEGIN
(0.1ms) COMMIT
I have two models: show_request and show. A show_Request belongs_to a show and a show has_many show_requests. On the show_request page in active_admin, I want to order show_requests by the show's created_at value. Here is my code so far:
ActiveAdmin.register ShowRequest do
controller do
def scoped_collection
end_of_association_chain.includes(:show)
#I also tried ShowRequest.includes(:show)
end
end
index do
column 'Show', sortable: "shows.created_at_asc" do |show_req|
link_to show_req.show.name, admin_show_path(show_req.show)
end
end
end
Here are the server logs:
Started GET "/admin/show_requests" for 127.0.0.1 at 2015-09-18 09:35:36 -0400
Processing by Admin::ShowRequestsController#index as HTML
AdminUser Load (0.3ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 1 ORDER BY "admin_users"."id" ASC LIMIT 1
(1.2ms) SELECT COUNT(*) FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't')
(0.2ms) SELECT COUNT(*) FROM "show_requests"
(0.2ms) SELECT COUNT(*) FROM "show_requests" WHERE (not_going_to_show = 't' AND i_want_my_horse_to_compete = 'f')
(0.3ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
CACHE (0.0ms) SELECT COUNT(*) FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't')
CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') LIMIT 30 OFFSET 0) subquery_for_count
ShowRequest Load (2.0ms) SELECT "show_requests".* FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') ORDER BY "show_requests"."id" desc LIMIT 30 OFFSET 0
Show Load (9.7ms) SELECT "shows".* FROM "shows" WHERE "shows"."id" IN (2, 1)
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Show Load (0.2ms) SELECT "shows".* FROM "shows"
User Load (0.2ms) SELECT "users".* FROM "users"
This is not working. It is not affecting the order of the columns at all. How do I fix this?
Take a look at this part of the log:
ShowRequest Load (2.0ms) SELECT "show_requests".* FROM "show_requests" WHERE (not_going_to_show = 'f' OR i_want_my_horse_to_compete = 't') ORDER BY "show_requests"."id" desc LIMIT 30 OFFSET 0
Show Load (9.7ms) SELECT "shows".* FROM "shows" WHERE "shows"."id" IN (2, 1)
You can see that the ShowRequests and Shows are loaded in separate queries. sortable is not going to work here, because you can't order one table by a field of another without a join. The fix should be to tell ActiveRecord that you need to reference the shows table in your query:
controller do
def scoped_collection
super.includes(:show).references(:shows)
end
end
index do
column :show, sortable: 'shows.created_at'
end
references only works with includes, and forces Rails to perform a join when eager loading.
This is really nothing more than just looking for some explanation around why the joins clause is necessary for what I am doing. Both return everything I want properly, but the queries are different and one is less optimal:
TL;DR: What is Rails doing exactly when I combine the joins with the includes, I wouldn't have thought I needed the joins really. Thanks for anyone willing to actually read through this monster.
Note: object is a ToDoListsUser object, and this is all happening in an exporter for what it's worth. And ProgressItem is a joins table between User and ToDoItem.
With the joins clause (queries look good and as expected):
object.to_do_list.progress_items.where(user_id: object.user_id)
.joins(:to_do_item)
.includes(:to_do_item)
Server output:
ToDoListsUser Load (0.5ms) SELECT "to_do_lists_users".* FROM "to_do_lists_users" WHERE "to_do_lists_users"."user_id" = $1 ORDER BY "to_do_lists_users"."id" DESC OFFSET 0 [["user_id", 543]]
ToDoList Load (0.3ms) SELECT "to_do_lists".* FROM "to_do_lists" WHERE "to_do_lists"."id" = $1 LIMIT 1 [["id", 144]]
SQL (1.6ms) SELECT "progress_items"."id" AS t0_r0, "progress_items"."completed" AS t0_r1, "progress_items"."user_id" AS t0_r2, "progress_items"."to_do_item_id" AS t0_r3, "progress_items"."created_at" AS t0_r4, "progress_items"."updated_at" AS t0_r5, "progress_items"."hidden" AS t0_r6, "to_do_items_progress_items"."id" AS t1_r0, "to_do_items_progress_items"."to_do_list_id" AS t1_r1, "to_do_items_progress_items"."description" AS t1_r2, "to_do_items_progress_items"."date_due" AS t1_r3, "to_do_items_progress_items"."created_at" AS t1_r4, "to_do_items_progress_items"."updated_at" AS t1_r5, "to_do_items_progress_items"."name" AS t1_r6, "to_do_items_progress_items"."element_id" AS t1_r7, "to_do_items_progress_items"."linkable_id" AS t1_r8, "to_do_items_progress_items"."linkable_type" AS t1_r9, "to_do_items_progress_items"."action" AS t1_r10 FROM "progress_items" INNER JOIN "to_do_items" "to_do_items_progress_items" ON "to_do_items_progress_items"."id" = "progress_items"."to_do_item_id" INNER JOIN "to_do_items" ON "progress_items"."to_do_item_id" = "to_do_items"."id" WHERE "to_do_items"."to_do_list_id" = $1 AND "progress_items"."user_id" = $2 ORDER BY to_do_items.created_at [["to_do_list_id", 144], ["user_id", 543]]
ToDoList Load (0.4ms) SELECT "to_do_lists".* FROM "to_do_lists" WHERE "to_do_lists"."id" = $1 LIMIT 1 [["id", 133]]
SQL (3.3ms) SELECT "progress_items"."id" AS t0_r0, "progress_items"."completed" AS t0_r1, "progress_items"."user_id" AS t0_r2, "progress_items"."to_do_item_id" AS t0_r3, "progress_items"."created_at" AS t0_r4, "progress_items"."updated_at" AS t0_r5, "progress_items"."hidden" AS t0_r6, "to_do_items_progress_items"."id" AS t1_r0, "to_do_items_progress_items"."to_do_list_id" AS t1_r1, "to_do_items_progress_items"."description" AS t1_r2, "to_do_items_progress_items"."date_due" AS t1_r3, "to_do_items_progress_items"."created_at" AS t1_r4, "to_do_items_progress_items"."updated_at" AS t1_r5, "to_do_items_progress_items"."name" AS t1_r6, "to_do_items_progress_items"."element_id" AS t1_r7, "to_do_items_progress_items"."linkable_id" AS t1_r8, "to_do_items_progress_items"."linkable_type" AS t1_r9, "to_do_items_progress_items"."action" AS t1_r10 FROM "progress_items" INNER JOIN "to_do_items" "to_do_items_progress_items" ON "to_do_items_progress_items"."id" = "progress_items"."to_do_item_id" INNER JOIN "to_do_items" ON "progress_items"."to_do_item_id" = "to_do_items"."id" WHERE "to_do_items"."to_do_list_id" = $1 AND "progress_items"."user_id" = $2 ORDER BY to_do_items.created_at [["to_do_list_id", 133], ["user_id", 543]]
vs.
Without the joins clause (queries are not optimal):
object.to_do_list.progress_items.where(user_id: object.user_id)
.includes(:to_do_item)
Server output:
ToDoListsUser Load (0.3ms) SELECT "to_do_lists_users".* FROM "to_do_lists_users" WHERE "to_do_lists_users"."user_id" = $1 ORDER BY "to_do_lists_users"."id" DESC OFFSET 0 [["user_id", 543]]
ToDoList Load (0.4ms) SELECT "to_do_lists".* FROM "to_do_lists" WHERE "to_do_lists"."id" = $1 LIMIT 1 [["id", 144]]
ProgressItem Load (2.0ms) SELECT "progress_items".* FROM "progress_items" INNER JOIN "to_do_items" ON "progress_items"."to_do_item_id" = "to_do_items"."id" WHERE "to_do_items"."to_do_list_id" = $1 AND "progress_items"."user_id" = $2 ORDER BY to_do_items.created_at [["to_do_list_id", 144], ["user_id", 543]]
ToDoList Load (0.5ms) SELECT "to_do_lists".* FROM "to_do_lists" WHERE "to_do_lists"."id" = $1 LIMIT 1 [["id", 133]]
ProgressItem Load (0.5ms) SELECT "progress_items".* FROM "progress_items" INNER JOIN "to_do_items" ON "progress_items"."to_do_item_id" = "to_do_items"."id" WHERE "to_do_items"."to_do_list_id" = $1 AND "progress_items"."user_id" = $2 ORDER BY to_do_items.created_at [["to_do_list_id", 133], ["user_id", 543]]
ToDoItem Load (0.4ms) SELECT "to_do_items".* FROM "to_do_items" WHERE "to_do_items"."id" IN (193, 194, 195, 196)
From ActiveRecord documentation:
conditions
If you want to add conditions to your included models you’ll have to explicitly reference them. For example:
User.includes(:posts).where('posts.name = ?', 'example')
Will throw an error, but this will work:
User.includes(:posts).where('posts.name = ?', 'example').references(:posts)
Note that includes works with association names while references needs the actual table name.
That happens, because actually Rails with include doesn't do join, it fetches records with two requests.
For example:
class List < ActiveRecord::Base
has_many :tasks
end
List.includes(:tasks)
# List Load (1.9ms) SELECT "lists".* FROM "lists"
# Task Load (0.8ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."list_id" IN (1, 2, 3)
But with references it falls back to join:
List.includes(:tasks).references(:tasks)
# SQL (0.6ms) SELECT "lists"."id" AS t0_r0, "lists"."name" AS t0_r1,
# "tasks"."id" AS t1_r0, "tasks"."name" AS t1_r1 FROM
# "lists" LEFT OUTER JOIN "tasks" ON "tasks"."list_id" = "lists"."id"
I have 2 models
Class Ride
has_many :trips
#state (string: active or expired)
end
Class Trip
#date (Date attribute)
scope :active, -> (start_at = Date.today) { where("trips.date >= ?", [Date.today, start_at].max) }
end
Daily, I need update state on Rides with active state having all trips with date attribute < Date.today
how to perform this in 1 query?
i can archive such result using:
Ride.with_active_state.select{|r| r.trips.active.size ==0}
but it makes huje queries to count trips, eq:
[1] pry(main)> Ride.with_active_state.select{|r| r.trips.active.size ==0}
(7.3ms) SELECT f_geometry_column,coord_dimension,srid,type FROM geometry_columns WHERE f_table_name='rides'
Ride Load (1.6ms) SELECT "rides".* FROM "rides" WHERE (rides.workflow_state = 'active')
(2.9ms) SELECT f_geometry_column,coord_dimension,srid,type FROM geometry_columns WHERE f_table_name='trips'
(1.3ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 9]]
(0.7ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 10]]
(0.7ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 11]]
(0.7ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 12]]
(0.8ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 13]]
(0.8ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 14]]
(0.5ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 15]]
(0.5ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 16]]
(0.5ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 17]]
(0.5ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 18]]
(0.5ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 19]]
(0.5ms) SELECT COUNT(*) FROM "trips" WHERE "trips"."ride_id" = $1 AND (trips.date >= '2013-09-24') [["ride_id", 20]]
....
Add scopes on Ride with a group and having clause. It would check the count of all future trips of a ride and return the rides with 0 count.
Class Ride
scope :active_state, where(state: "active")
scope :with_nonactive_trips, -> (start_date = Date.today){ joins(:trips).
group("rides.id").
having( ["sum(trips.date > ?) = 0",start_date] ) }
end
Ride.active_state.with_nonactive_trips
# returns All the rides with state == active, alteast one trip and having no trips with date > Date.today
Using a lambda since you had it on the active scope in Trip. I am guessing you need to use a different date than Date.today for some queries.