Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 553 Bytes

2022-07-08_use-scoping-to-apply-a-scope-to-a-block.md

File metadata and controls

27 lines (19 loc) · 553 Bytes

Use scoping to apply a scope to a block

Say you have a scope in an Active Record model like:

scope :published, -> { where(published: true) }

And for some reason you want the scope to be applied to all the queries in a block, you need to use scoping:

Model.published.scoping do
  Model.first # will use the scope
end

Because doing this is essentially useless:

Model.published do
  Model.first # will use the scope
end