Skip to content

Instantly share code, notes, and snippets.

@ljkbennett
Created September 5, 2013 09:37
Show Gist options
  • Save ljkbennett/6448027 to your computer and use it in GitHub Desktop.
Save ljkbennett/6448027 to your computer and use it in GitHub Desktop.
Recorded for posterity
scope :for_region, lambda { |region|
joins("LEFT OUTER JOIN blog_post_shared_regions ON blog_post_shared_regions.blog_post_id = blog_posts.id")
.where("blog_posts.region_id = ? OR (blog_post_shared_regions.region_id = ? AND blog_post_shared_regions.visible = true)", region.id, region.id)
.order( "CASE WHEN blog_post_shared_regions.shared_at IS NULL THEN blog_posts.updated_at WHEN blog_post_shared_regions.shared_at > blog_posts.updated_at THEN blog_post_shared_regions.shared_at ELSE blog_posts.updated_at END DESC")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment