Skip to content

Rails library to check if an ActiveRecord can be destroyed

License

Notifications You must be signed in to change notification settings

JDrizzy/activerecord-destroyable

Repository files navigation

This gem allows you to use destroy? for rails ActiveRecord models checking to see if a record can be destroyed.

Usage

Example scenario:

class User < ApplicationRecord
  has_many :posts, dependent: :destroy
end

class Post < ApplicationRecord
  belongs_to :user

  has_many :comments, dependent: :restrict_with_error
end

class Comment < ApplicationRecord
  belongs_to :post
end

Calling destroy will not return the expected Post error Cannot delete record because dependent comments exist:

user = User.find(1)
if !user.destroy # => false
  puts user.errors.full_messages.to_sentence # => ""
end

Calling destroy? will:

user = User.find(1)
if user.destroy? # => false
  user.destroy # => true
else
  puts user.errors.full_messages.to_sentence # => Cannot delete record because dependent comments exist
end

Installation

Add this line to your application's Gemfile:

gem 'activerecord-destroyable', '~> 0.1.0'

And then execute:

$ bundle update

Or install it yourself as:

$ gem install activerecord-destroyable

Real talk

Ideally a consistent design pattern should be encouraged. If you are running into the usage scenario then you should probably use :restrict_with_error to prevent the scenario e.g.

class User < ApplicationRecord
  has_many :posts, dependent: :restrict_with_error
end

class Post < ApplicationRecord
  belongs_to :user

  has_many :comments, dependent: :restrict_with_error
end

class Comment < ApplicationRecord
  belongs_to :post
end

I have yet to use this on a production application!

License

The gem is available as open source under the terms of the MIT License.

About

Rails library to check if an ActiveRecord can be destroyed

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published