Skip to content

Instantly share code, notes, and snippets.

@JoshTGreenwood
Created March 24, 2016 15:26
Show Gist options
  • Save JoshTGreenwood/0e693d25996f51200229 to your computer and use it in GitHub Desktop.
Save JoshTGreenwood/0e693d25996f51200229 to your computer and use it in GitHub Desktop.
Active Record Merge

TIL #merge is a thing on AR models.

Useful for joining + using a named scoped on the joined table:

class Post; end
class Comment; scope :approved, -> {approved: true}; end
Post.joins(:comments).merge(Comment.approved)

vs.

Post.joins(:comments).where(comments: {approved: true})  # this requires duplicating the logic defined in your scope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment