簡體   English   中英

rails-在開始/救援中執行多種模型方法

[英]rails - Executing multiple model methods in a begin/rescue

我有多個模型方法,我想循環並執行每個方法。 我將如何在Rails 2.3.11中執行此操作? 最好是在開始/救援中。

編輯:

謝謝mapprihoda,我使用了您的示例,並能夠將其應用於begin / rescue:

class MyModel
  def method_1
    puts 'In method_1'
  end

  def method_2
    puts 'In method_2'
  end

  def method_3
    %w( method_1 method_2).each { |m| 
      begin
        self.send(m)
      rescue => e
        puts "#{e.message}"
      end
    }
  end
end

像這樣嗎

class MyModel
  def method_1
    puts 'In method_1'
  end

  def method_2
    puts 'In method_2'
  end

  def method_3
    %w( method_1 method_2).each { |m| self.send(m) }
  end
end

my_model = MyModel.new
my_model.method_3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM