1

I have this Ruby code that I want to use:

if args[:remove_existing_trxs] == 'true'
    Acquirer.delete_all
    Company.delete_all
    Currency.delete_all
    AdminUser.delete_all
    BaseReseller.delete_all
    Terminal.delete_all
    Contract.delete_all
    Merchant.delete_all
    MerchantUser.delete_all
    PaymentTransaction.delete_all
  end

How can I define it as an array and iterate?

2
  • 3
    Wouldn't dependency_true on each model (with defined model association) be more appropriate on this scenario and use one delete_all to delete the dependencies? Commented Sep 12, 2017 at 7:56
  • 2
    Why did you tag your question with ruby-on-rails-3 and ruby-on-rails-4, are you targeting both versions? Commented Sep 12, 2017 at 7:58

2 Answers 2

4

Something like that?

[Model1, Model2].each do |model|
  model.public_send(:delete_all)
end 

Or with using Symbol#to_proc:

[Model1, Model2].each(&:delete_all)
Sign up to request clarification or add additional context in comments.

Comments

2

try this out:

  if args[:remove_existing_trxs] == 'true'
    [Acquirer, Company, Currency, AdminUser,
    BaseReseller, Terminal, Contract, Merchant,
    MerchantUser, PaymentTransaction].each(&:destroy_all)
  end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.