Travis Pessetto
Travis Pessetto

Reputation: 3298

Rails destroy method leaving values in other tables

I have a rails app that has two models one is vender and the other is purchase_request where vender has has_many :purchase_requests and purchase_request has belongs_to :vender yet when I call destroy it will not delete fields with this vender in it, and thus crashes the application because it returns nil objects where this vender use to be. how can I fix this?

Upvotes: 0

Views: 74

Answers (2)

jschorr
jschorr

Reputation: 3054

Try this code, it should work for you:

  has_many :purchase_requests, :dependent => :delete_all

Upvotes: 3

Wizard of Ogz
Wizard of Ogz

Reputation: 12643

It sounds like you need to use the :dependent => :destroy option for you association.

class Vendor < AR::Base
  has_many :purchase_requests, :dependent => :destroy
end

Upvotes: 4

Related Questions