我有一个具有下列列的表名事务
created_at | debit | credit | balance | account_id
2012-2-2 02:22:22 | 3000 | 0 | 0 | 8
2012-2-2 07:22:22 | 500 | 1000 | 0 | 8
2012-2-2 09:22:22 | 0 | 8000 | 0 | 8
2012-3-3 19:17:20 | 1000 | 0 | 0 | 8
2012-3-3 04:02:22 | 0 | 8000 | 0 | 8在计算余额之前,我需要
我有100万个事务由account_id来区分。在这种情况下,什么是有效的排序方法?
如有任何答复,将不胜感激。
发布于 2012-12-28 14:21:53
这听起来很像this question
Transaction.all(:order => "created_at DESC, debit DESC")这种查询正是关系数据库所擅长的,并且有了适当的索引,对您来说应该是有效的。
对于特定帐户…
Transaction.find_by_account_id(some_id, :order => "created_at DESC, debit DESC")如果将此设置为帐户上的适当ActiveRecord关联,则可以在那里设置订单,以便始终以首选的顺序返回关联:
class Account
has_many :transactions, :order => "created_at DESC, debit DESC"
end这样,任何时候,当您要求一个帐户的事务,如Account.find(12345).transactions,他们将返回在首选排序自动。
https://stackoverflow.com/questions/14068836
复制相似问题