order active_admin column by parent item in belongs_to relationship - ruby-on-rails

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.

Related

terminal output for Rails app produces double requests

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

Eagerload objects with has_many has_many association ANd where not all associations exist

I have 2 models: Deal and User with a many_to_many relations and a user_deal joined table.
On the homepage i display a list of Deals. For each Deal I use inside the view n attribute that is inside the user_deal table.
How can I eager load this ?
I tried to do this:
homepage controller
def home
#deals = Deal.includes(:user_deals)
respond_to do |format|
format.html # home.html.erb
format.json { render json: #deals }
format.xml { render xml: #deals }
# format.atom
end
end
end
Models
class Deal < ActiveRecord::Base
has_many :user_deals, dependent: :destroy
has_many :users, through: :user_deals
end
class User < ActiveRecord::Base
has_many :user_deals
has_many :deals, through: :user_deals
end
class UserDeal < ActiveRecord::Base
belongs_to :user, :foreign_key => 'user_id'
belongs_to :deal, :foreign_key => 'deal_id'
end
But I feel it's not working as it's preloading things but then loading each object a second time...
Processing by StaticPagesController#home as HTML
(0.5ms) SELECT COUNT(*) FROM "Deals" AND "Deals"."featured" = 't'
Deal Load (0.7ms)
SELECT "Deals".* FROM "Deals" WHERE "Deals"."country" = $1 AND "Deals"."featured" = 't' ORDER BY "Deals"."Deal_end_date" ASC
// it seems here below to implement the preloading
UserDeal Load (1.2ms) SELECT "user_deals".* FROM "user_Deals" WHERE "user_Deals"."Deal_id" IN (30, 339, 341, 337, 353, 31, 354, 260)
//but then it's loading things individually again
UserDeal Load (0.6ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 30) LIMIT 1
UserDeal Load (0.6ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 339) LIMIT 1
UserDeal Load (0.7ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 341) LIMIT 1
UserDeal Load (0.7ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 337) LIMIT 1
UserDeal Load (0.6ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 353) LIMIT 1
UserDeal Load (0.7ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 31) LIMIT 1
UserDeal Load (0.9ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 354) LIMIT 1
UserDeal Load (0.6ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 260) LIMIT 1
My assumption of where the problem comes from (I'm not sure)
The problem might be inside the User_deal table. It works this way: if a user participated in a deal, a new line is created with user_id = 5, deal_id= 4, number_of_participations = 6
So if the user 4 never participated in a deal 7, there is no line user_id= 4 and deal_id = 7
So on the homepage, Rails go to fetch the list of deals: deal 1, deal 2...and deal 7.
But he does not find the line where deal_7 as there is no such line.
Ideally, I should tell Rails to includes(:user_deals) but only those where inside the joined table UserDeals where user_id= current_user.id OR where the line with user_id = current_user.id does NOT exist...which I did not manager to implement anyway
but I'm a RoR rookie so I'm not sure of my ideas here above
EDIT
Following suggestion to try eager_load instead of includes, I get:
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 625]]
Processing by StaticPagesController#home as HTML
(0.7ms) SELECT COUNT(DISTINCT "deals"."id") FROM "deals" LEFT OUTER JOIN "user_deals" ON "user_deals"."deal_id" = "deals"."id" WHERE "deals"."country" = $1 AND (deal_launch_date <= '2016-05-02 17:21:59.547029' AND deal_end_date >= '2016-05-02 17:21:59.547087') AND "deals"."featured" = 't' [["country", "France"]]
SQL (3.0ms) SELECT "deals"."id" AS t0_r0, "deals"."country" AS t0_r1, "deals"."title" AS t0_r2, "deals"."deal_card_edito" AS t0_r3, "deals"."twitter_msg" AS t0_r4, "deals"."image_url" AS t0_r5, "deals"."prelaunch_date" AS t0_r6, "deals"."featured" AS t0_r9, "deals"."admin_user_id" AS t0_r10, "deals"."created_at" AS t0_r11, "deals"."updated_at" AS t0_r12,........................................................................................................
"user_deals"."id" AS t1_r0, "user_deals"."user_id" AS t1_r1, "user_deals"."deal_id" AS t1_r2, "user_deals"."number_participations" AS t1_r3, "user_deals"."nb_new_clicks" AS t1_r4, "user_deals"."created_at" AS t1_r5, "user_deals"."updated_at" AS t1_r6 FROM "deals" LEFT OUTER JOIN "user_deals" ON "user_deals"."deal_id" = "deals"."id" WHERE "deals"."country" = $1 AND (deal_launch_date <= '2016-05-02 17:21:59.547029' AND deal_end_date >= '2016-05-02 17:21:59.547087') AND "deals"."featured" = 't' ORDER BY "deals"."deal_end_date" ASC [["country", "France"]]
UserDeal Load (0.9ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 30) LIMIT 1
UserDeal Load (1.0ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 339) LIMIT 1
UserDeal Load (0.6ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 341) LIMIT 1
UserDeal Load (0.6ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 337) LIMIT 1
UserDeal Load (0.9ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 353) LIMIT 1
UserDeal Load (1.0ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 31) LIMIT 1
UserDeal Load (0.5ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 354) LIMIT 1
UserDeal Load (0.9ms) SELECT "user_deals".* FROM "user_deals" WHERE (user_id = 625 AND deal_id = 260) LIMIT 1

Active model serializer - wrong number of arguments (0 for 1)

I'm trying to serialize a nested collection. I have a product with images. But when I try a has_many or an explicit method to define an images attribute in my product serializer, I get:
wrong number of arguments (0 for 1)
I could have sworn this was doable. Am I doing something wrong?
Here is my code:
class Api::V1::ProductSerializer < ActiveModel::Serializer
attributes :id, :sku, :name, :description, :images
def images
object.images
end
end
Full backtrace
Started GET "/api/v1/products" for 10.0.2.2 at 2015-03-02 23:12:34 +0000
Processing by Api::V1::ProductsController#index as HTML
User Load (0.7ms) SELECT "users".* FROM "users" INNER JOIN "tenant_users" ON "tenant_users"."user_id" = "users"."id" WHERE "tenant_users"."tenant_id" = 1 AND "users"."authentication_token" = 'pGJE5UWrxzJTEPxMyiez' ORDER BY "users"."id" ASC LIMIT 1
(0.3ms) BEGIN
SQL (0.4ms) UPDATE "users" SET "current_sign_in_at" = $1, "last_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = 1 [["current_sign_in_at", "2015-03-02 23:12:34.913333"], ["last_sign_in_at", "2015-03-02 23:12:33.754114"], ["sign_in_count", 171], ["updated_at", "2015-03-02 23:12:34.914298"]]
(0.7ms) COMMIT
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."tenant_id" = 1
Attachinary::File Load (0.5ms) SELECT "attachinary_files".* FROM "attachinary_files" WHERE "attachinary_files"."attachinariable_id" = $1 AND "attachinary_files"."attachinariable_type" = $2 AND "attachinary_files"."scope" = 'images' [["attachinariable_id", 1], ["attachinariable_type", "Product"]]
Completed 500 Internal Server Error in 19ms
ArgumentError (wrong number of arguments (0 for 1)):
app/controllers/api/v1/products_controller.rb:4:in `index'
app/controllers/concerns/api/landlord.rb:13:in `require_tenant!'
Rendered /usr/local/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.4ms)
Rendered /usr/local/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms)
Rendered /usr/local/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.1ms)
Rendered /usr/local/rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.7ms)

active record quering in rails 4 and postgres

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.

Rails 2 + spree (0.11.0): Where is the CheckoutsController?

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.

Resources