Create Query Scopes
Define a Scope
struct Post
include CQL::ActiveRecord::Model(Int64)
db_context MyDB, :posts
property id : Int64?
property title : String
property published : Bool = false
property views_count : Int64 = 0
property created_at : Time?
# Scopes
def self.published
where(published: true)
end
def self.draft
where(published: false)
end
def self.popular(min_views = 100)
where { views_count >= min_views }
end
def self.recent(days = 7)
where { created_at > days.days.ago }
end
endUse Scopes
Scope with Parameters
Conditional Scopes
Default Scope Pattern
Scope Chaining
Scope with Ordering
Complex Scopes
Scope in Related Queries
Verify Scopes Work
Related
Last updated
Was this helpful?