I see that named scopes can be used to inject additional AND
conditions into the WHERE component of a query, however is there any
way to inject an OR condition?
Say for example I have a Company and Office model, the Company
has_many Offices and both the Company and Office own many child
The Office should be able to see the child models that he specifically
owns, but also those owned by his parent company that have been
designated to be shared throughout the company.
So what I really want to do is something like:
@items = current_office.items.including_shared
where the including_shared scope would have to inject a condition like
‘OR (company_id = x AND shared = 1)’ into the query.
However I can’t see a way to do this.
I can get the results I want from either:
@items = current_office.items
@items << current_company.items.shared
or using a single scope on the Item class:
However I don’t like that the first one requires two db calls, and for
the second one I prefer to start all fetches with either
current_company/office so that I can easily tell that they are
properly scoped to the current application instance.
Anyway I’m probably being a bit anal, but just wondered if anyone has