我有一张表,transactions
,可能包含重复项(对于我们来说,副本是Transaction
,具有相同的account_id
,date
和amount
)。如何在AREL中加入表格以查找重复项?
我的英语语言功能的要求是:“我希望看到其中有具有相同ACCOUNT_ID,日期和金额存在超过1个交易的所有交易”。
上AREL放弃暂时,我公司生产的SQL是这样的:
SELECT * FROM transactions t1, transactions t2
WHERE t1.id != t2.id
AND t1.date = t2.date
AND t1.amount = t2.amount
AND t1.account_id = t2.account_id
我使用Rails的3.2.x中和Postgres。
本来,我在AREL试过这样:
Transaction.group(:account_id, :date, :amount).having("count(id) > 1")
但是,这给了我有关集合函数的SQL错误:
PG::Error: ERROR: column "transactions.id" must appear in the GROUP BY clause or be used in an aggregate function
..这是令人沮丧的,因为我做不希望在group by子句中使用ID - 整个问题是我希望在检查dupe时忽略ID。
我很感激,如果有人能指出我正确的方向AREL我需要使这个范围 - find_by_sql
是伟大的,当你需要记录,但我想创建一个ActiveAdmin范围 - 它不喜欢数组。
我不介意downvotes,但我介意downvotes没有意见,为什么我downvoted? – makdad 2013-02-21 02:13:15
可能是个人资料照片:P – pchap10k 2013-02-21 02:27:00