Skip to content

Instantly share code, notes, and snippets.

@alloy
Created October 12, 2011 13:17
Show Gist options
  • Save alloy/1281203 to your computer and use it in GitHub Desktop.
Save alloy/1281203 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