【发布时间】:2016-07-24 14:46:36
【问题描述】:
场景是,我想获取 2 个案例的所有时间段。
如果门卫为真,则查询将相同
如果 doorman 为 false 则需要在查询中添加参数
因此,几乎相同的查询几乎可以在两种情况下都适用,只需稍作修改。
这是查询和代码:
def self.latest_pickup_date current_zone,doorman
if doorman
latest_timeslot = Timeslot.where(dropoff_slots: '-1', zone_id: current_zone).order(:slot_date).last
else
latest_timeslot = Timeslot.where(dropoff_slots: '-1', zone_id: current_zone, doorman_type: "none").order(:slot_date).last
end
latest_timeslot.nil? ? Date.current : latest_timeslot.slot_date
end
我想以使用 DRY 方法的方式重构我的代码和查询。
我不想在这两种情况下都写两次这些查询。我需要使用代码实践的更好解决方案。或者,如果我这样做是正确的,您也可以提出建议。
如果有人可以提供帮助,还需要良好的专业代码实践和代码重构。
【问题讨论】:
-
您的问题标题是无法重构,为什么无法重构?你知道codereview.stackexchange.com吗?
-
@Зелёный 我没有那么多好的知识,而且我的专业知识有限,所以我发帖以获得专家的答案。我的目的不仅仅是得到答案。我自己也可以。好吧,我不知道 codereview.stackexchange.com
-
你应该把这个问题发到codereview.stackexchange.com
标签: ruby-on-rails postgresql ruby-on-rails-4