squeel
squeel copied to clipboard
Where clause with join, group and sum generates ERROR in Rails 4.1.8
Config: rails 4.1.8 squeel-1.2.2 The following squeel code works:
# == Schema Information
#
# Table name: bank_accounts
#
# id :integer not null, primary key
# number :string(255)
# currency_id :integer
# created_at :datetime
# updated_at :datetime
# opening_balance :decimal(10, 2)
# opening_date :date
class BankAccount < ActiveRecord::Base
has_many :accounts
scope :trial_balance, -> {
BankAccount.
joins{accounts.transactions}.
group{[number, opening_date]}.
sum(:amount)
}
end
However, when a where clause is applied to limit the resul set it fails
scope :trial_balance, -> {
BankAccount.
where{transactions.date.gt_eq bank_accounts.opening_date}.
joins{accounts.transactions}.
group{[number, opening_date]}.
sum(:amount)
}
with the following error: neither group nor sum are recognised:
irb(main):055:0> BankAccount.trial_balance
NoMethodError: BankAccount Load (0.1ms) SELECT `bank_accounts`.* FROM `bank_accounts` INNER JOIN `accounts` ON `accounts`.`bank_account_id` = `bank_accounts`.`id` INNER JOIN `transactions` ON `transactions`.`account_id` = `accounts`.`id`
undefined method `group' for #<ActiveRecord::QueryMethods::WhereChain:0x007f610e427310>
from /home/christopher/work/trust-accounts/app/models/bank_account.rb:23:in `block in <class:BankAccount>'
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/scoping/named.rb:151:in `call'
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/scoping/named.rb:151:in `block (2 levels) in scope'
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/relation.rb:292:in `scoping'
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.1.8/lib/active_record/scoping/named.rb:151:in `block in scope'
from (irb):55
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/console.rb:90:in `start'
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start'
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/christopher/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/railties-4.1.8/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
The SQL it should generate is perfectly legal, as in
select number, opening_date, sum(t.amount)
from bank_accounts b, accounts a, transactions t
where b.id = a.bank_account_id and a.id = t.account_id and t.date >= b.opening_date
group by number, opening_date
Would appreciate very much if you could take a look & point out any errors in my syntax or suggest how this query may otherwise be written using squeel
Regards