سؤال

I am building a rails applications, where in a part of it, I save to the database, payments information. I need to make these payments un-deleteable.

I have created a before_destroy function that kinda works.. but I am having an issue:

here is my code:

class StripePayment < ActiveRecord::Base
belongs_to :user
belongs_to :stripe_card

before_destroy :fail

private
def fail
    return false
end
end

When I create a payment and try out my code when delete:

StripePayment.first.destroy returns false and rollback... Which is exactly what I want.

However, StripePayment.first.delete passes and deletes the object.

I know the deference between delete and destroy. however, I want to be able to prevent this object from being deleted on the DB (on both delete() and destroy() calls.

I tried before_delete and rails gave me back this error:

NoMethodError: undefined method `before_delete' for #<Class:0x007fc1abc37c50>
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
from /Users/alybadawy/developing/repos/finish/finish/app/models/stripe_payment.rb:7:in `<class:StripePayment>'
from /Users/alybadawy/developing/repos/finish/finish/app/models/stripe_payment.rb:1:in `<top (required)>'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `load'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `block in load_file'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:615:in `new_constants_in'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:422:in `load_file'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:323:in `require_or_load'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:462:in `load_missing_constant'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
from (irb):1
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'

Any help will be appreciated. Thanks :)

هل كانت مفيدة؟

المحلول

The easiest way is, to define your own delete method

class StripePayment < ActiveRecord::Base
  belongs_to :user
  belongs_to :stripe_card

  before_destroy :fail

  def delete
    false
  end

  private
  def fail
      return false
  end
end

If you have conditions, where you want to allow deleteion, you can check for it iy your delete method and call super

نصائح أخرى

that's because delete is one of the functions that skips callbacks in rails

check this out for more info on the subject http://edgeguides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top