Skip to content

Instantly share code, notes, and snippets.

@kapkaev
Forked from alloy/gist:1281203
Created October 25, 2012 13:09
Show Gist options
  • Save kapkaev/3952451 to your computer and use it in GitHub Desktop.
Save kapkaev/3952451 to your computer and use it in GitHub Desktop.
ActiveRecord::Relation#merge example
class Edition < ActiveRecord::Base
scope :published, where(arel_table[:published_at].not_eq(nil))
scope :generated, where(arel_table[:generated_at].not_eq(nil))
scope :available, published.generated
end
class Magazine < ActiveRecord::Base
# This is where #merge works its magic, by allowing you to merge scopes from other models.
scope :with_available_editions, includes(:editions).merge(Edition.available)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment