Skip to content

Instantly share code, notes, and snippets.

@jgelo
Created June 12, 2011 18:27
Show Gist options
  • Save jgelo/1021853 to your computer and use it in GitHub Desktop.
Save jgelo/1021853 to your computer and use it in GitHub Desktop.
In Rails 3.1, count is ignoring the include specified in my default_scope in BudgetAccount -
class Budget
has_many :accounts, :class_name => 'BudgetAccount', :as => :poly, :dependent => :destroy
end
class BudgetAccount
belongs_to :account
belongs_to :poly, :polymorphic => true
default_scope where('accounts.inactive = ?', false).order('accounts.number').includes(:account)
end
class Account
end
Rails 3.0 works as expected -
count = Budget.first.accounts.where('total > 100000').count
SELECT COUNT(DISTINCT "budget_accounts"."id") FROM "budget_accounts" LEFT OUTER JOIN "accounts" ON
"accounts"."id" = "budget_accounts"."account_id" WHERE (accounts.inactive = 'f') AND
("budget_accounts".poly_id = 1 AND "budget_accounts".poly_type = 'Budget') AND (total > 100000)
Rails 3.1 breaks -
count = Budget.first.accounts.where('total > 100000').count
SELECT COUNT(*) FROM "budget_accounts" WHERE "budget_accounts"."poly_id" = 1 AND "budget_accounts"."poly_type" = 'Budget' AND
(accounts.inactive = 'f') AND (total > 100000)
ActiveRecord::StatementInvalid: PGError: ERROR: missing FROM-clause entry for table "accounts"
LINE 1: ...AND "budget_accounts"."poly_type" = 'Budget' AND (accounts.i...
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment