0
class Foo < ActiveRecord::Base
  has_many: bars, dependent: :destroy
  has_one: roo, dependent: :destroy
end

class Bar < ActiveRecord::Base
  has_many: bazs, dependent: :destroy
end

class Baz < ActiveRecord::Base
end

class Roo < ActiveRecord::Base
end

I am trying to remove Foo with the following code, but I am getting an FK exception while removing baz references from bar/

Is my syntax for removing roo correct?

Foo.delete(foo_id)
Joe
  • 14,513
  • 28
  • 82
  • 144

2 Answers2

1

Try this:

@foo = Foo.find(foo_id)
@foo.destroy
Ganesh Kunwar
  • 2,643
  • 2
  • 20
  • 36
1

Ref this to understand the use of the dependent: :destroy, destroy & delete, so you have to use destroy method :destroy/:destroy_all The associated objects are destroyed alongside this object

@foo.destroy
Community
  • 1
  • 1
Salil
  • 46,566
  • 21
  • 122
  • 156