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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7069221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:51:47  来源:igfitidea点击:

Delete a record from console -- Ruby on Rails

mysqlruby-on-railsruby

提问by kill007

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.

但是当我退出控制台并通过 script/console 命令返回控制台时,我可以再次看到 id = 1 的记录。

The database is not local and I'm connecting through database.yml by giving an ip.

数据库不是本地的,我通过提供 ip 通过 database.yml 进行连接。

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

谢谢

回答by vandrop

Try

尝试

User.find(1).destroy

This should work in the console and otherwise.

这应该在控制台中工作,否则。

回答by Adam Hammouda

On a similar note you can remove all the tuples for a given Object from the rails console as follows:

在类似的说明中,您可以从 rails 控制台中删除给定对象的所有元组,如下所示:

@object = Object.all

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

回答by Dinesh Pallapa

Easy way to remove Models data from console

从控制台删除模型数据的简单方法

rails console

Store user model to an instance variable

将用户模型存储到实例变量

user = User.find(1)

delete or destroy the relations by following commands

通过以下命令删除或破坏关系

user.delete

or

或者

user.destroy

回答by webaholik

Make sure you aren't invoking console in sandbox mode, which will prevent the delete operation.

确保您没有在沙箱模式下调用控制台,这将阻止删除操作。

回答by LukeBickleTWA

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

用户优先销毁