在数据库对象的位置 [英] Position of object in database
本文介绍了在数据库对象的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有模型团队
和我有(即)团队= Team.first:偏移=> 20
。现在,我需要的位置号我小组
中的数据库表。
I have got model Team
and I've got (i.e.) team = Team.first :offset => 20
. Now I need to get number of position of my team
in db table.
我可以在Ruby中做到这一点:
I can do it in ruby:
Team.all.index team #=> 20
不过,我相信,我可以与大表写在SQL,这将是更便宜的我。
But I am sure that I can write it on SQL and it will be less expensive for me with big tables.
推荐答案
假设顺序由编号递减发:
Assuming the order is made by ID desc:
class Team < ActiveRecord::Base
def position
self.class.count(:conditions => ['id <= ?', self.id])
end
end
这篇关于在数据库对象的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文