我在 Rails Controller 中有以下代码:
@users = User.where(["first_name = :first_name or
last_name = :last_name or
company = :company",
{ first_name: term, last_name: term,
company: term }])
术语是 term = params[:search]
我不喜欢我重复 term
{ first_name: term, last_name: term,
company: term }
是否有一种 DRYER 方法可以做到这一点?
谢谢!
最佳答案
你可以这样做:
@users = User.where("first_name = :term OR
last_name = :term OR company = :term", term: term)
关于ruby-on-rails - 编写具有重复值的 Ruby Hash 而不重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25770285/