diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-24 17:26:32 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-24 17:26:32 -0800 |
commit | ecd072d21951573f59e9515b868224d3732dbdfa (patch) | |
tree | b58b2c8e14c991206b2989f5478476cc9d7419c9 | |
parent | 51fdd769c0cb096d6e6f04afc3ebb91833d625bc (diff) | |
download | rails-ecd072d21951573f59e9515b868224d3732dbdfa.tar.gz rails-ecd072d21951573f59e9515b868224d3732dbdfa.tar.bz2 rails-ecd072d21951573f59e9515b868224d3732dbdfa.zip |
renamed attribute to operand per josh's suggestion
-rw-r--r-- | README | 27 | ||||
-rw-r--r-- | lib/active_relation/predicates.rb | 25 |
2 files changed, 29 insertions, 23 deletions
@@ -12,33 +12,33 @@ you construct a table relation and convert it to sql: ActiveRelation::Table.new(:users).to_sql -In fact, you will probably never call `#to_sql` directly. Let `users = ActiveRelation::Table.new(:users)`. You can iterate through all rows in the `users` table like this: +In fact, you will probably never call `#to_sql`. Let `users = ActiveRelation::Table.new(:users)`. Rather, you'll work with data from the table directly. You can iterate through all rows in the `users` table like this: users.each { |user| ... } -In other words, Arel relations behave similar regular Ruby collections. Let's have a look at a concrete example: +In other words, Arel relations behave implement Ruby's Eunmerable interface. Let's have a look at a concrete example: users.first # => {'id' => 10, 'name' => 'bob'} -As you can see, Arel converts the rows into a hash, the values of which are the appropriate Ruby primitive (integers, strings, and so forth). +As you can see, Arel converts the rows from the database into a hash, the values of which are sublimated to the appropriate Ruby primitive (integers, strings, and so forth). == Relational Algebra == -Arel is based on the Relational Algebra, a mathematical model that is also the inspiration for relational databases. ActiveRelation::Relation objects do not represent queries per se (i.e., they are not object-representations of `SELECT`, `INSERT`, `UPDATE`, or `DELETE`), rather they represent a collection of data that you can select from, insert into, update, and delete.For example, to insert a row into the users table, do the following: +Arel is based on the Relational Algebra, a mathematical model that is also the inspiration for relational databases. ActiveRelation::Relation objects do not represent queries per se (i.e., they are not object-representations of `SELECT`, `INSERT`, `UPDATE`, or `DELETE` statements), rather they represent a collection of data that you can select from, insert into, update, and delete. For example, to insert a row into the users table, do the following: - users.insert({'id' => 11, 'name' => 'amy'}) + users.insert({users[:name] => 'amy'}) # => INSERT INTO users (users.name) VALUES ('amy') To delete all users: - users.delete + users.delete # => DELETE FROM users To update: - users.update({'name' => 'carl'}) + users.update({users[:name] => 'carl'}) # => UPDATE users SET name = 'carl' -As you can see, the `relation` users does not represent an individual query; rather it is an abstraction on a collection of data and it can produce appropriate queries to do the various CRUD operations. +As you can see, the `relation` named `users` does not represent an individual query; rather it is an abstraction on a collection of data and it can produce appropriate SQL queries to do the various CRUD operations. -=== More Sophisticated Queries === +=== More Sophisticated <strike>Queries</strike> Relations === Following the Relational Algebra, Arel's interface uses some jargon that differs from standard SQL. For example, in order to add a `WHERE` clause to your relations, you use the `select` operation: @@ -47,13 +47,18 @@ Following the Relational Algebra, Arel's interface uses some jargon that differs What would, in SQL, be part of the `SELECT` clause is called here a `projection`: users.project(users[:id]) # => SELECT users.id FROM users + +Joins are fairly straightforward: + + users.join(photos).on(users[:id].equals(photos[:user_id])) => SELECT * FROM users INNER JOIN photos ON users.id = photos.user_id -The best property of the Relational is compositionality, or closure under all operations. For example, to combine the two previous queries: +The best property of the Relational is compositionality, or closure under all operations. For example, to select and project: users \ .select(users[:name].equals('amy')) \ - .project(users[:id]) + .project(users[:id]) \ # => SELECT users.id FROM users WHERE users.name = 'amy' +s == Contributions == diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index c4f7b83512..7d0618f42e 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -6,14 +6,15 @@ module ActiveRelation end class Binary < Predicate - attr_reader :attribute, :operand + # rename "operand21", "operand22" + attr_reader :operand1, :operand2 - def initialize(attribute, operand) - @attribute, @operand = attribute, operand + def initialize(operand1, operand2) + @operand1, @operand2 = operand1, operand2 end def ==(other) - super and @attribute == other.attribute and @operand == other.operand + super and @operand1 == other.operand1 and @operand2 == other.operand2 end def bind(relation) @@ -25,19 +26,19 @@ module ActiveRelation end def to_sql(strategy = Sql::Predicate.new) - "#{attribute.to_sql(strategy)} #{predicate_sql} #{operand.to_sql(strategy)}" + "#{operand1.to_sql(strategy)} #{predicate_sql} #{operand2.to_sql(strategy)}" end def descend - self.class.new(yield(attribute), yield(operand)) + self.class.new(yield(operand1), yield(operand2)) end end class Equality < Binary def ==(other) self.class == other.class and - ((attribute == other.attribute and operand == other.operand) or - (attribute == other.operand and operand == other.attribute)) + ((operand1 == other.operand1 and operand2 == other.operand2) or + (operand1 == other.operand2 and operand2 == other.operand1)) end protected @@ -75,15 +76,15 @@ module ActiveRelation end class Match < Binary - alias_method :regexp, :operand + alias_method :regexp, :operand2 - def initialize(attribute, regexp) - @attribute, @regexp = attribute, regexp + def initialize(operand1, regexp) + @operand1, @regexp = operand1, regexp end end class RelationInclusion < Binary - alias_method :relation, :operand + alias_method :relation, :operand2 protected def predicate_sql |