使用if/then/else条件编写Ruby方法的最简单方法是什么?

Kev*_*ell 1 ruby activerecord ruby-on-rails

我在ActiveRecord模型类中有这个类级方法.

def self.is_animal_color_correct?(animal, color)
  if AnimalColor.find_by_animal_and_color(animal.downcase, color.downcase) 
    true
  else
    false
  end
end
Run Code Online (Sandbox Code Playgroud)

我只是想知道格式化方法的最佳方法是什么.这看起来很冗长,但很清楚.

Soh*_*han 7

在这个特定的例子中,我认为这是你想要的:

AnimalColor.exists?(:animal => animal.downcase, :color => color.downcase) 
Run Code Online (Sandbox Code Playgroud)

一般情况下,只要您检查真相,即任何不是空或假的东西,您都不应该在意.