Reputation: 2643
You can see the last lines of these two models use the same code:
class Position < ActiveRecord::Base
scope :last_day,
where('positions.created_at > ? AND positions.hidden = ?',
DateTime.now.in_time_zone("Sofia").yesterday.beginning_of_day + 10.hours, false)
end
class Subscriber < ActiveRecord::Base
scope :notify_today,
joins(:tags => :positions).
where('positions.created_at > subscribers.created_at
AND positions.created_at > ? AND positions.hidden = ?',
DateTime.now.in_time_zone("Sofia").yesterday.beginning_of_day + 10.hours, false)
end
Is it possible to reuse the 'last_day' scope somehow in the second model?
Upvotes: 2
Views: 2269
Reputation: 7480
where()
and joins()
both return ActiveRecord::Relation
objects, and they have a method called merge()
which can merge other ActiveRecord::Relation
objects. Therefore, you can simply do this
scope :notify_today, joins(:tags => :positions).merge(Position.last_day)
Also, &
is an alias to merge()
, so you should also be able to do this
scope :notify_today, joins(:tags => :positions) & Position.last_day
Upvotes: 2