Pregunta

I have a rake task to destroy records of a model older than a certain time. Even though the records are getting deleted, but the associations are not.

The rake task is simple:

Product.where("marked = ? AND created_at < ?", false, 15.days.ago).destroy_all

The associations:

Product Model:

has_many :features, :dependent => :destroy

Feature Model:

belongs_to :product
¿Fue útil?

Solución

Just Try:

@products_to_destroy = Product.where("marked = ? AND created_at < ?", false, 15.days.ago)

@products_to_destroy.destroy_all

OR

Product.destroy_all("marked = ? AND created_at < ?", false, 15.days.ago)

Ref: http://apidock.com/rails/ActiveRecord/Base/destroy_all/class

Hope it helps :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top