Created
August 2, 2016 10:00
-
-
Save ceritium/ff01f68591794339b899f4a845af0c9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Remittance < ActiveRecord::Base | |
has_many :debits | |
scope :add_debits_amount, -> { group("remittances.id").joins(:debits).select("remittances.*, sum(debits.amount) AS _debits_amount") } | |
end | |
class Debit < ActiveRecord::Base | |
belongs_to :remittance | |
end | |
Remittance.add_debits_amount.map(&:_debits_amount) #=> [ ... ] Works | |
Remittance.add_debits_amount.count # => ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*, sum(debits.amount) AS _debits_amount) AS count_remittances_all_sum_debits_amo' at line 1: SELECT COUNT(remittances.*, sum(debits.amount) AS _debits_amount) AS count_remittances_all_sum_debits_amount_as__debits_amount, remittances.id AS remittances_id FROM `remittances` INNER JOIN `debits` ON `debits`.`remittance_id` = `remittances`.`id` AND `debits`.`deleted_at` IS NULL WHERE `remittances`.`deleted_at` IS NULL GROUP BY remittances.id | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment