In my Rails app, I want to join one table with a named scope of another table. Is there a way to do this without having to rewrite the named scope in pure SQL for my join statement?
Basically, is there a way to do something like this?
class Foo < ActiveRecord::Base
scope :updated_today, where('updated_at > ?', DateTime.now.prev_day)
end
Bar.joins(Foo.updated_today)
Where Bar.joins generates the following SQL:
SELECT * FROM bars
INNER JOIN
(SELECT * FROM foos WHERE updated_at > 2012-8-9) AS t0
ON bar_id = bars.id