Created
May 18, 2012 12:19
-
-
Save silasb/2724995 to your computer and use it in GitHub Desktop.
Sequel Model
This file contains hidden or 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
| require "sequel" | |
| # connect to an in-memory database | |
| DB = Sequel.sqlite | |
| # create an items table | |
| DB.create_table :people do | |
| primary_key :id | |
| String :last_name | |
| end | |
| peoples = DB[:people] | |
| # populate the table | |
| peoples.insert(:last_name => 'Bridge') | |
| peoples.insert(:last_name => 'Snyder') | |
| peoples.insert(:last_name => 'Frank') | |
| class Person < Sequel::Model | |
| dataset_module do | |
| def with_last_initial(initial) | |
| where(:last_name.ilike("#{initial}%")).all | |
| end | |
| end | |
| end | |
| # usage | |
| Person.with_last_initial('B') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment