简体   繁体   English

从控制台删除一条记录 — Ruby on Rails

[英]Delete a record from console — Ruby on Rails

I want to delete a record from the console.我想从控制台中删除一条记录。

I logged through script/console and我通过脚本/控制台登录并

User.find(1).delete
$<User id: 1, ..............>
User.find(1)
ActiveRecord::RecordNotFound: Couldn't find User with ID=1
........
........
........

Everything looks fine till here.到这里为止一切看起来都很好。

But when I exit the console and return back to console by script/console command, I am able again to see the record with id = 1.但是当我退出控制台并通过脚本/控制台命令返回控制台时,我可以再次看到 id = 1 的记录。

The database is not local and I'm connecting through database.yml by giving an ip.数据库不是本地的,我通过 database.yml 提供 ip 进行连接。

when I try to do the same on the development local database, everything is fine.当我尝试在开发本地数据库上做同样的事情时,一切都很好。

Can someone please explain whats happening and where am I missing!有人可以解释发生了什么,我在哪里失踪!

Thanks谢谢

Try尝试

User.find(1).destroy

This should work in the console and otherwise.这应该在控制台和其他地方工作。

On a similar note you can remove all the tuples for a given Object from the rails console as follows:在类似的注释中,您可以从 rails 控制台中删除给定 Object 的所有元组,如下所示:

@object = Object.all

@object.each do |o|
  o.delete
end

Easy way to remove Models data from console从控制台中删除模型数据的简单方法

rails console

Store user model to an instance variable将用户 model 存储到实例变量

user = User.find(1)

delete or destroy the relations by following commands通过以下命令删除或破坏关系

user.delete

or或者

user.destroy

Try User.count or y User.count尝试 User.count 或 y User.count

User.first where first is the number you want to delete User.first 其中 first 是您要删除的号码

User.first.destroy User.first.destroy

Make sure you aren't invoking console in sandbox mode, which will prevent the delete operation.确保您没有在沙盒模式下调用控制台,这将阻止删除操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM