Today I had some knowledge delivered straight to the top of my dome, by Darcy. The issue was I had two models that I wanted to join and then apply scopes to. I wanted to retrieve Story objects that were joined to HourEntry objects, that were from this week. However when I tried to use the Time scope on the query it was attempting to call those methods on Story.
ree-1.8.7-2011.02 :132 > Story.joins(:hour_entries).this_week NoMethodError: undefined method `this_week' for #
The problem was that the .this_week call was being sent to Story rather than to HourEntry. Here’s where Darcy chimed in with the scoped.merge call. It went a little something like this :
This successfully merged the scope defined by Story with the scope defined by the HourEntry.this_week call and I get all the stories that have HourEntry objects that are returned by the .this_week scope.
The final stage for this call was :
This then returns all the stories you’ve worked on this week. Add a bit of grouping/distinct and it’s done and done. The additional nice part about this is composing these scopes will allow me to do a lot more in the future, very quickly.