我有3種型號:添加自類模型軌道3
Class Model1
end
Class Model2
end
Class Model3
end
我有這樣的代碼:
scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_time...end_time)) }
class << self
## Class methods for calculating searches
def created_today
today = Time.zone.now
created_between(today.beginning_of_day, today.end_of_day)
end
def created_yesterday
yesterday = Time.zone.now - 1.day
created_between(yesterday.beginning_of_day, yesterday.end_of_day)
end
def created_last_week
start_time = (Time.zone.now - 1.week).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
def created_last_month
start_time = (Time.zone.now - 1.month).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
def created_last_year
start_time = (Time.zone.now - 1.year).beginning_of_day
end_time = Time.zone.now
created_between(start_time, end_time)
end
末
我需要這個代碼添加到3款,但我不不想在每個模型中重複代碼。
我該怎麼辦?
非常感謝!
我不會爲第一種方法使用範圍。你可以用另一個帶有兩個參數的類方法來實現,然後你不需要lamba。 –
謝謝,你能添加一個例子嗎? – hyperrjas
只要完成你對其他方法的操作,但是該方法需要兩個參數。 –