参数列表过长

2020-07-12  本文已影响0人  SecondRocker
def query(name,age,gender,enter_time,leave_time,level,poisition)
  cond = {}
  cond.merge! name: name if name.present?
  cond.merge! gendar: gender if gender.present?
  ...
  Employee.where(cond)
end

# 修改为

def cond(key,value)
  @cond || = {}
  @cond.merge key: value
end

def query
  yield(self) if block_given?
end

# 调用
query do |q|
  q.cond name: name if name.present?
  q.cond age: age if age.present?
  ..
end


上一篇 下一篇

猜你喜欢

热点阅读