我有两个模型,如下所述.

class EmpGroup < ActiveRecord::Base
  belongs_to :user
  has_many :emp_group_members, dependent: :destroy
end

class EmpGroupMember < ActiveRecord::Base
  belongs_to :emp_group
  belongs_to :user
end

现在的问题是,每当我试图摧毁一个团体,我就会收到如下错误.

PG::ForeignKeyViolation: ERROR:  update or delete on table "emp_groups" violates foreign key constraint "fk_rails_bd68440021" on table "emp_group_members"
DETAIL:  Key (id)=(1) is still referenced from table "emp_group_members".

我错过了什么?

推荐答案

将级联删除添加到您的EmpGroup型号:

class EmpGroup < ActiveRecord::Base
   has_many :emp_group_members, dependent: :delete_all
end

你要打delete吗?你应该打destroy.

Ruby-on-rails相关问答推荐

仅当未在带有ENV[';Dyno';]的Heroku问题上运行时才初始化Sidekiq Cron

如何修改使用Google Cloud App Engine部署rails 7应用程序的步骤

Rails TurboStream:可以向元素动态添加一个css类吗?

如何在嵌套路由中使用Ruby on rails多态关联

如何通过graphqlMutations 从rails销毁当前用户会话

has_many 通过大小验证

为什么 Rails 执行器不能识别内核的改进?

Rails 100% 新手问题 - send() 方法

如何在 IRB/Rails 控制台中 suppress 返回值的输出?

Ruby 地理定位Ruby /插件

Rails:设计:按角色登录后重定向

是否可以在 Rails 中否定范围?

Ruby on Rails:有条件地显示部分

如何解决弃用警告方法 to_hash 已弃用并将在 Rails 5.1 中删除

如何在特定时区(最好是我的应用程序的默认时区,而不是 UTC)中创建新的 DateTime 对象?

如何在 ROR (Ruby) 中显示 PDF?

Rails SQL 正则表达式

:javascript haml 标签中的内联Ruby ?

Ruby:如何将一种方法接收到的所有参数和块传递给另一种方法?

Rails 3:乘客找不到Bundle 程序安装的 git gem