实现数组并迭代

时间:2017-09-12 07:49:28

标签: ruby ruby-on-rails-3 ruby-on-rails-4

我想要使用这个Ruby代码:

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

如何将其定义为数组并进行迭代?

2 个答案:

答案 0 :(得分:4)

那样的东西?

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

或使用Symbol#to_proc

[Model1, Model2].each(&:delete_all)

答案 1 :(得分:2)

试试这个:

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