活动记录 - 获取数据库中的第二个,第三个...项目(无ID) [英] Active Record - Get the second, third.. item in a database (without ID)

查看:179
本文介绍了活动记录 - 获取数据库中的第二个,第三个...项目(无ID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检索数据库中的第二个,第三个..条目。我不想使用由rails生成的自动递增ID。

How to I retrieve the second, third .. entries in a database. I don't want to use the auto incrementing id generated by rails.

由于我从数据库中删除条目,因此ID继续增加。

As I am deleting entries from my database, so the ID continues to increase.

所以我的数据库会有

id : 210 (Even thought it's currently the first value)
id : 211 (Even thought it's currently the second value)

Phone.first将返回第一个如何获取第二个。

I know "Phone.first" will return the first how do I get the second.

子问题

Destroy_all / delete_all -
只删除项目,我可以从数据库中删除所有条目,并使数据库ID从一个开始,而不删除表。

Sub Question-
Destroy_all/delete_all -
only removes the item, Can I remove all entries from the DB and have the DB id's start from one without dropping the table. As I ran into problems.

推荐答案

假设你想要第四个用户:

Say you want the fourth user:

 @users = User.limit(4)
 @fourth_user = @users[3]

关于你的第二个问题, destroy_all 应该做的技巧,因为它触发所有回调。请务必添加:dependent => :destroy

Concerning your second question, destroy_all should do the trick since it triggers all callbacks. Be sure to add :dependent => :destroy in your relationships.

这篇关于活动记录 - 获取数据库中的第二个,第三个...项目(无ID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆