How do I 'validate' on destroy in rails - ruby-on-rails

On destruction of a restful resource, I want to guarantee a few things before I allow a destroy operation to continue? Basically, I want the ability to stop the destroy operation if I note that doing so would place the database in a invalid state? There are no validation callbacks on a destroy operation, so how does one "validate" whether a destroy operation should be accepted?

You can raise an exception which you then catch. Rails wraps deletes in a transaction, which helps matters.
For example:
class Booking < ActiveRecord::Base
has_many :booking_payments
....
def destroy
raise "Cannot delete booking with payments" unless booking_payments.count == 0
# ... ok, go ahead and destroy
super
end
end
Alternatively you can use the before_destroy callback. This callback is normally used to destroy dependent records, but you can throw an exception or add an error instead.
def before_destroy
return true if booking_payments.count == 0
errors.add :base, "Cannot delete booking with payments"
# or errors.add_to_base in Rails 2
false
# Rails 5
throw(:abort)
end
myBooking.destroy will now return false, and myBooking.errors will be populated on return.

just a note:
For rails 3
class Booking < ActiveRecord::Base
before_destroy :booking_with_payments?
private
def booking_with_payments?
errors.add(:base, "Cannot delete booking with payments") unless booking_payments.count == 0
errors.blank? #return false, to not destroy the element, otherwise, it will delete.
end

It is what I did with Rails 5:
before_destroy do
cannot_delete_with_qrcodes
throw(:abort) if errors.present?
end
def cannot_delete_with_qrcodes
errors.add(:base, 'Cannot delete shop with qrcodes') if qrcodes.any?
end

State of affairs as of Rails 6:
This works:
before_destroy :ensure_something, prepend: true do
throw(:abort) if errors.present?
end
private
def ensure_something
errors.add(:field, "This isn't a good idea..") if something_bad
end
validate :validate_test, on: :destroy doesn't work: https://github.com/rails/rails/issues/32376
Since Rails 5 throw(:abort) is required to cancel execution: https://makandracards.com/makandra/20301-cancelling-the-activerecord-callback-chain
prepend: true is required so that dependent: :destroy doesn't run before the validations are executed: https://github.com/rails/rails/issues/3458
You can fish this together from other answers and comments, but I found none of them to be complete.
As a sidenote, many used a has_many relation as an example where they want to make sure not to delete any records if it would create orphaned records. This can be solved much more easily:
has_many :entities, dependent: :restrict_with_error

The ActiveRecord associations has_many and has_one allows for a dependent option that will make sure related table rows are deleted on delete, but this is usually to keep your database clean rather than preventing it from being invalid.

You can wrap the destroy action in an "if" statement in the controller:
def destroy # in controller context
if (model.valid_destroy?)
model.destroy # if in model context, use `super`
end
end
Where valid_destroy? is a method on your model class that returns true if the conditions for destroying a record are met.
Having a method like this will also let you prevent the display of the delete option to the user - which will improve the user experience as the user won't be able to perform an illegal operation.

I ended up using code from here to create a can_destroy override on activerecord:
https://gist.github.com/andhapp/1761098
class ActiveRecord::Base
def can_destroy?
self.class.reflect_on_all_associations.all? do |assoc|
assoc.options[:dependent] != :restrict || (assoc.macro == :has_one && self.send(assoc.name).nil?) || (assoc.macro == :has_many && self.send(assoc.name).empty?)
end
end
end
This has the added benefit of making it trivial to hide/show a delete button on the ui

You can also use the before_destroy callback to raise an exception.

I have these classes or models
class Enterprise < AR::Base
has_many :products
before_destroy :enterprise_with_products?
private
def empresas_with_portafolios?
self.portafolios.empty?
end
end
class Product < AR::Base
belongs_to :enterprises
end
Now when you delete an enterprise this process validates if there are products associated with enterprises
Note: You have to write this in the top of the class in order to validate it first.

Use ActiveRecord context validation in Rails 5.
class ApplicationRecord < ActiveRecord::Base
before_destroy do
throw :abort if invalid?(:destroy)
end
end
class Ticket < ApplicationRecord
validate :validate_expires_on, on: :destroy
def validate_expires_on
errors.add :expires_on if expires_on > Time.now
end
end

I was hoping this would be supported so I opened a rails issue to get it added:
https://github.com/rails/rails/issues/32376

Related

Rails 5 dependent: :destroy doesn't work

I have the following classes:
class Product < ApplicationRecord
belongs_to :product_category
def destroy
puts "Product Destroy!"
end
end
class ProductCategory < ApplicationRecord
has_many :products, dependent: :destroy
def destroy
puts "Category Destroy!"
end
end
Here, I am trying to override the destroy method where I eventually want to do this:
update_attribute(:deleted_at, Time.now)
When I run the following statement in Rails console: ProductCategory.destroy_all I get the following output
Category Destroy!
Category Destroy!
Category Destroy!
Note: I have three categories and each category has more than one Products. I can confirm it by ProductCategory.find(1).products, which returns an array of products. I have heard the implementation is changed in Rails 5. Any points on how I can get this to work?
EDIT
What I eventually want is, to soft delete a category and all associated products in one go. Is this possible? Or will ave to iterate on every Product object in a before destroy callback? (Last option for me)
You should call super from your destroy method:
def destroy
super
puts "Category destroy"
end
But I definitely wouldn't suggest that you overide active model methods.
So this is how I did it in the end:
class Product < ApplicationRecord
belongs_to :product_category
def destroy
run_callbacks :destroy do
update_attribute(:deleted_at, Time.now)
# return true to escape exception being raised for rollback
true
end
end
end
class ProductCategory < ApplicationRecord
has_many :products, dependent: :destroy
def destroy
# run all callback around the destory method
run_callbacks :destroy do
update_attribute(:deleted_at, Time.now)
# return true to escape exception being raised for rollback
true
end
end
end
I am returning true from the destroy does make update_attribute a little dangerous but I am catching exceptions at the ApplicationController level as well, so works well for us.

Rails soft delete child record

Rails 4.2
I have a parent class that accepts nested attributes for its children.
class Parent < ActiveRecord::Base
has_many :kids
accepts_nested_attributes_for :kids, allow_destroy: true
end
class Kid < ActiveRecord::Base
belongs_to :parent
def destroy
if some_count > 0
self.hidden = true
else
self.destroy
end
end
end
I sometimes want to set the hidden flag on the child instead of deleting it. I am doing this via accepts_nested_attributes_for. I need this decision to be set on the server side, I can't have users deciding whether to destroy or hide.
But not destroying raises ActiveRecord::RecordNotDestroyed - Failed to destroy the record:
What's the right way to do this?
The error is thrown because you got into infinite loop (you are calling destroy method within destroy method). Use super instead. Also you need to save hidden column change to the database. In this case it should be safe to use update_column (no validation and no callbacks are triggered, no other columns are saved to the database)
class Kid < ActiveRecord::Base
belongs_to :parent
def destroy
if some_count > 0
update_column(:hidden, true)
else
super
end
end
end
To answer other question you need to explain what some_count is. :)

How can I skip active record validations in some special cases?

I am having a RoR application for human resource planning. Normaly it is not allowed to delete entries from the past. From time to time I have to delete users and of course I want to delete all entries.
My user model:
has_many :team_memberships, dependent: :destroy
has_many :slots, through: :team_memberships, source: :slot
TeamMembership model:
before_destroy :check_permission
def check_permission
if self.slot.start_time < Time.now.beginning_of_day
errors.add(:base, "Historische Einträge können nicht gelöscht werden")
return false
end
end
A rake task just destroys the old users, but that does not work, when they have historical entries.
How can I skip the before_destroy validation when User.destroy is called by a rake task?
Any ideas?
You could add a new method to your model that set a variable and use that as a return value of the callback:
before_destroy :check_permission
def allow_deletion!
#allow_deletion = true
end
def check_permission
if !#allow_deletion && self.slot.start_time < Time.now.beginning_of_day
errors.add(:base, "Historische Einträge können nicht gelöscht werden")
return false
end
end
That allows you to write something like this:
user.allow_deletion!
user.destroy
This method has the benefit over delete that you only skip this specific callback and not all callbacks and dependent destroy operations.
You can call user.delete which will skip the callbacks.

validate the presence of at least one association object in rails 3.2

I've one small problem, I can't get solved. I want to validate that there is at least one associated model. Like in the following
class User < ActiveRecord::Base
has_many :things
validates_presence_of :things
end
class Thing < ActiveRecord::Base
belongs_to :user
end
This works fine when I update my model via #update_attributes, but when I simply set #user.things = [], I am able to get invalid data in the database. My workaroud to solve this is to overwrite the setter method
def things=(val)
begin
if val.blank?
errors.add(:things, "not valid")
raise SomeError
end
super
rescue SomeError
false
end
end
But somehow this doesn't feel right. Isn't there a way to archive the same result via validations and/or callbacks, preferably so that #things= return false (and not val) and so that #user.things is not changed (I mean the cached #user.things, #user.things(true) should work fine anyway).
You can create a custom validator that will check the presence of things.
Instead of
validates_presence_of :things
You could do
validate :user_has_things
def user_has_things
if self.things.size == 0
errors.add("user has no thingies")
end
end

Validate presence of nested attributes

How do I validate that a model has at least one associated model using nested attributes? This has been driving me crazy as I am sure that I am missing something simple. For example, I want to require that a List always has at least one Task.
class List < ActiveRecord::Base
has_many :tasks, :dependent => :destroy
accepts_nested_attributes_for :tasks, :allow_destroy => true
end
class Task < ActiveRecord::Base
belongs_to :list
end
I've tried many different options.
1- adding a validation to lists:
def validate
if self.tasks.length < 1
self.errors[:base] << "A list must have at least one task."
end
end
but this will still allow you to delete all the tasks of an existing list since when deleting tasks the validation of list happens before the tasks are destroyed.
2- checking to see if any tasks are not marked for destruction in a before_save callback
before_save :check_tasks
private
#look for any task which won't be deleted
def check_tasks
for t in self.tasks
return true if ! t.marked_for_destruction?
end
false
end
For some reason I can't get it to ever delete a task with anything that iterates over a list's tasks. The same is true if I do this check in def validate instead of a callback
3- requiring the presence of tasks validates_presence_of :tasks, but with this it won't ever delete any tasks
You can check both conditions together in validation method:
validate :check_tasks
def check_tasks
if self.tasks.size < 1 || self.tasks.all?{|task| task.marked_for_destruction? }
errors.add_to_base("A list must have at least one task.")
end
end
I ended up extending Magazine's save method to get around the problem. It worked like a charm.
def save
saved = false
ActiveRecord::Base.transaction do
saved = super
if self.conditions.size < 1
saved = false
errors[:base] << "A rule must have at least one condition."
raise ActiveRecord::Rollback
end
end
saved
end

Resources