Created
February 22, 2013 01:18
-
-
Save pjb3/5010023 to your computer and use it in GitHub Desktop.
It would be cool if you could define methods on a model that return a filter and chain them, the way you can in ActiveRecord
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
| $ ruby sequel_filter_chaining.rb | |
| I, [2013-02-21T20:16:43.689560 #45206] INFO -- : (0.000207s) PRAGMA foreign_keys = 1 | |
| I, [2013-02-21T20:16:43.689785 #45206] INFO -- : (0.000045s) PRAGMA case_sensitive_like = 1 | |
| I, [2013-02-21T20:16:43.690238 #45206] INFO -- : (0.000347s) CREATE TABLE `people` (`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT, `gender` varchar(255), `born_on` date) | |
| I, [2013-02-21T20:16:43.691695 #45206] INFO -- : (0.000569s) PRAGMA table_info('people') | |
| I, [2013-02-21T20:16:43.697758 #45206] INFO -- : (0.000217s) SELECT sqlite_version() LIMIT 1 | |
| I, [2013-02-21T20:16:43.698478 #45206] INFO -- : (0.000071s) BEGIN | |
| I, [2013-02-21T20:16:43.703150 #45206] INFO -- : (0.000148s) INSERT INTO `people` (`gender`, `born_on`) VALUES ('m', '1992-02-27') | |
| I, [2013-02-21T20:16:43.706520 #45206] INFO -- : (0.002898s) SELECT * FROM `people` WHERE (`id` = 1) LIMIT 1 | |
| I, [2013-02-21T20:16:43.706702 #45206] INFO -- : (0.000058s) COMMIT | |
| I, [2013-02-21T20:16:43.707035 #45206] INFO -- : (0.000033s) BEGIN | |
| I, [2013-02-21T20:16:43.707483 #45206] INFO -- : (0.000107s) INSERT INTO `people` (`gender`, `born_on`) VALUES ('m', '1995-02-26') | |
| I, [2013-02-21T20:16:43.708044 #45206] INFO -- : (0.000218s) SELECT * FROM `people` WHERE (`id` = 2) LIMIT 1 | |
| I, [2013-02-21T20:16:43.708200 #45206] INFO -- : (0.000049s) COMMIT | |
| I, [2013-02-21T20:16:43.708432 #45206] INFO -- : (0.000029s) BEGIN | |
| I, [2013-02-21T20:16:43.708760 #45206] INFO -- : (0.000079s) INSERT INTO `people` (`gender`, `born_on`) VALUES ('f', '1992-02-27') | |
| I, [2013-02-21T20:16:43.709234 #45206] INFO -- : (0.000171s) SELECT * FROM `people` WHERE (`id` = 3) LIMIT 1 | |
| I, [2013-02-21T20:16:43.709383 #45206] INFO -- : (0.000046s) COMMIT | |
| I, [2013-02-21T20:16:43.709658 #45206] INFO -- : (0.000035s) BEGIN | |
| I, [2013-02-21T20:16:43.710075 #45206] INFO -- : (0.000099s) INSERT INTO `people` (`gender`, `born_on`) VALUES ('f', '1995-02-26') | |
| I, [2013-02-21T20:16:43.710552 #45206] INFO -- : (0.000144s) SELECT * FROM `people` WHERE (`id` = 4) LIMIT 1 | |
| I, [2013-02-21T20:16:43.710685 #45206] INFO -- : (0.000044s) COMMIT | |
| sequel_filter_chaining.rb:27:in `<main>': undefined method `underage' for #<Sequel::SQLite::Dataset:0x007fb2f8eb9d28> (NoMethodError) |
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 'logger' | |
| require 'sequel' | |
| db = Sequel.sqlite(':memory:', :logger => Logger.new(STDOUT)) | |
| db.create_table :people do | |
| primary_key :id | |
| String :gender | |
| Date :born_on | |
| end | |
| class Person < Sequel::Model(db) | |
| def self.male | |
| where(:gender => 'm') | |
| end | |
| def self.underage | |
| where{born_on < (Time.now - (60 * 60 * 24 * 365 * 21))} | |
| end | |
| end | |
| Person.create(:gender => 'm', :born_on => (Time.now - (60 * 60 * 24 * 365 * 21))) | |
| Person.create(:gender => 'm', :born_on => (Time.now - (60 * 60 * 24 * 365 * 18))) | |
| Person.create(:gender => 'f', :born_on => (Time.now - (60 * 60 * 24 * 365 * 21))) | |
| Person.create(:gender => 'f', :born_on => (Time.now - (60 * 60 * 24 * 365 * 18))) | |
| puts Person.male.underage.count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment