【问题标题】:Destroying records based on condition根据条件销毁记录
【发布时间】:2022-12-18 09:38:55
【问题描述】:

我正试图破坏我的负债表中的一条记录。在这个表中有一个 person 和 page 字段。我正在尝试查找 ID 为 5 且显示页面的责任记录。

person = person.find_by_id(5)
page =  Liability.where(page: 'show')
if person == 5
 page.destroy
end

这不会删除记录。为什么?

【问题讨论】:

  • Liability.where(page: 'show') 返回 ActiveRecord 关联,而不是模型。需要 Liability.where(page: 'show').first
  • 你是说destroy_all
  • destroy_all 也不会删除记录

标签: ruby-on-rails ruby


【解决方案1】:

Liability.where(page: 'show') 返回数组关联而不是 object 在这种情况下你应该使用page.destroy_all

【讨论】:

  • 我刚刚试过 destroy_all。当我检查数据库时,没有记录被删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
相关资源
最近更新 更多