./README in ambition-0.1.2 vs ./README in ambition-0.1.3
- old
+ new
@@ -32,15 +32,18 @@
puts user.name
end
And that's it.
-The key is the +each+ method. You build up a +Query+ using +select+, +first+,
-and +sort_by+, then call +each+ on it. This'll run the query and enumerate
-through the results. Really, you can use any Enumerable method: +map+,
-+each_with_index+, etc.
+The key is that queries arent actually run until the data they represent is
+requested. Usually this is done with what I call a kicker method. You can call them
+that, too.
+Kicker methods are guys like +detect+, +each+, +each_with_index+, +map+, +entries+,
++to_a+, and +first+ (with no argument). Methods like +select+, +sort_by+, and +first+
+(with an argument) are not kicker methods and return a +Query+ object without running any SQL.
+
Our +Query+ object has two useful methods: +to_sql+ and +to_hash+. With these,
we can check out what exactly we're building. Not everyone has +to_sql+,
though. Mostly ignore these methods and treat everything like you normally
would.
@@ -125,11 +128,11 @@
== #detect
User.detect { |m| m.name == 'chris' }
"SELECT * FROM users WHERE users.`name` = 'chris' LIMIT 1"
-== LIMITs -- first, first(x), [offset, limit], [range]
+== LIMITs -- first, first(x), [offset, limit], [range], slice
User.select { |m| m.name == 'jon' }.first
"SELECT * FROM users WHERE users.`name` = 'jon' LIMIT 1"
User.select { |m| m.name == 'jon' }.first(5)
@@ -170,9 +173,18 @@
User.select { |m| m.name == 'jon' }.size
SELECT count(*) AS count_all FROM users WHERE (users.`name` = 'jon')
>> User.select { |m| m.name == 'jon' }.size
=> 21
+
+== Other Enumerables
+
+ These methods perform COUNT() operations rather than loading your array into memory. They're all
+ kickers.
+
+ User.any? { |m| m.name == 'jon' }
+ User.all? { |m| m.name == 'jon' }
+ User.select { |m| m.name == 'jon' }.empty?
== SELECT * FROM bugs
Found a bug? Sweet. Add it at the Lighthouse: http://err.lighthouseapp.com/projects/466-plugins/tickets/new