Reputation: 2665
I'm trying to create a scope that's a little out of the ordinary. I've got an app that deals with websites. There's a model called "sites", which has_many pages. The sites model has a column called homepage. The list of pages for each site contains the homepage and additional pages (about, pricing, whatever).
I'd like to create two scopes for the pages model. One identifies the homepage and the other identifies all the additional pages. I was thinking of something like this.
my_homepage = Site.find_by_id(self.site_id).homepage
scope :homepage, where(url: my_homepage)
scope :additional_pages, where(url != my_homepage)
Unfortunately, these don't work. In fact, when I try to play with them in console, I can't. I get this error.
(Object doesn't support #inspect)
Is there a smarter way to do this?
Upvotes: 0
Views: 74
Reputation: 7240
Try this :
scope :homepage, joins(:sites).where("sites.homepage = pages.url")
scope :additional_pages, joins(:sites).where("sites.homepage <> pages.url")
Upvotes: 1