Skip to content

Instantly share code, notes, and snippets.

@a2f0
Last active January 4, 2016 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a2f0/8553481 to your computer and use it in GitHub Desktop.
Save a2f0/8553481 to your computer and use it in GitHub Desktop.
destroy always fail
Here is the command that I can execute against it (the one that I think I should *not* be able to execute. I basically am trying to write a more complex validation that prevents deletion of a tag based on more complex criteria, but would like to first write a base before_destroy validation that causes the deletion to *always* fail no matter what.
@command = Command.find(params[:id])
tag = Tag.find(params[:tag_id])
@command.tags.delete(tag)
Here is the complete model for tagmapsorter:
class Tagmapsorter < ActiveRecord::Base
belongs_to :command
belongs_to :tag
before_destroy :always_fail
def always_fail
return false
end
end
Here is the complete model for command:
class Command < ActiveRecord::Base
has_many :tagmapsorters
has_many :tags, through: :tagmapsorters
validates :text, presence: true
validates :description, presence: true
validates :text, uniqueness: true
validate :has_tags?
def has_tags?
errors.add(:base, 'Must have at least one tag.') if self.tags.blank?
end
end
And the complete model for Tag:
class Tag < ActiveRecord::Base
has_many :tagmapsorters
has_many :commands, through: :tagmapsorters
validates :tag, presence: true
validates :tag, uniqueness: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment