-
-
Save srpouyet/99fb635ed310bfb35629f7cb0c17baed to your computer and use it in GitHub Desktop.
The Sequel Gem: Everything About Scopes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (I recommend understanding the basics of this first: http://sequel.jeremyevans.net/rdoc/files/doc/object_model_rdoc.html) | |
# Extending the underlying dataset (http://sequel.jeremyevans.net/rdoc/files/README_rdoc.html#label-Extending+the+underlying+dataset) | |
# The recommended way to implement table-wide logic by defining methods on the dataset using dataset_module: | |
class Post < Sequel::Model | |
dataset_module do | |
def posts_with_few_comments | |
where{num_comments < 30} | |
end | |
def clean_posts_with_few_comments | |
posts_with_few_comments.delete | |
end | |
end | |
end | |
# This allows you to have access to your model API from filtered datasets as well: | |
Post.where(:category => 'ruby').clean_posts_with_few_comments | |
# Sequel models also provide a subset class method that creates a dataset method with a simple filter: | |
class Post < Sequel::Model | |
subset(:posts_with_few_comments){num_comments < 30} | |
subset :invisible, Sequel.~(:visible) | |
end | |
# Model ((http://www.rubydoc.info/gems/sequel/Sequel/Model)) | |
DATASET_METHODS : Class methods added to model that call the method of the same name on the dataset | |
Dataset::ACTION_METHODS + Dataset::QUERY_METHODS | |
# Model::ClassMethods (http://www.rubydoc.info/gems/sequel/Sequel/Model/ClassMethods) | |
#subset(name, *args, &block) ⇒ Object | |
# Sets up a dataset method that returns a filtered dataset. Sometimes thought of as a scope, and like most dataset methods, they can be chained. For example: | |
Topic.subset(:joes, :username.like('%joe%')) | |
Topic.subset(:popular){num_posts > 100} | |
Topic.subset(:recent){created_on > Date.today - 7} | |
# Allows you to do `Topic.joes.recent.popular` to get topics with a username that includes joe that have more than 100 posts and were created less than 7 days ago. | |
# Both the args given and the block are passed to Dataset#filter. | |
# This method creates dataset methods that do not accept arguments. | |
# To create dataset methods that accept arguments, you should use define a method directly inside a #dataset_module block. | |
# Model::DatasetModule (http://www.rubydoc.info/gems/sequel/Sequel/Model/DatasetModule) | |
# This Module subclass is used by Model.dataset_module to add dataset methods to classes. | |
# It adds a couple of features standard Modules, allowing you to use the same subset method you can call on Model, | |
# as well as making sure that public methods added to the module automatically have class methods created for them. | |
#subset(name, *args, &block) ⇒ Object | |
# Define a named filter for this dataset, see Model.subset for details. | |
# ActiveRecord -- named_scope (http://sequel.jeremyevans.net/rdoc/files/doc/active_record_rdoc.html#label-named_scope) | |
# For a pure filter, you can use subset: | |
Album.subset(:debut, :position => 1) | |
Album.subset(:gold){copies_sold > 500000} | |
# For anything more complex, you can use dataset_module: | |
Album.dataset_module do | |
def by_artist(artist_id) | |
where(:artist_id=>artist_id) | |
end | |
def by_release_date | |
order(:release_date) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment