From f183668ff92f220183f58d6779126a11feaf4d04 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:35:17 +1000 Subject: Querying guide: Add mention of the scope method --- .../guides/source/active_record_querying.textile | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index b9ad7ccbd2..fe6d284239 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -721,6 +721,48 @@ h4. Specifying Conditions on Eager Loaded Associations Even though Active Record lets you specify conditions on the eager loaded associations just like +joins+, the recommended way is to use "joins":#joining-tables instead. +h3. Scopes + +Scoping allows you to specify commonly-used ARel queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as +where+, +joins+ and +includes+. + +To define a simple scope, we use the +scope+ method inside the class, passing the ARel query that we'd like run when this scope is called: + + + class Post < ActiveRecord::Base + scope :published, where(:published => true) + end + + +Just like before, these methods are also chainable: + + + class Post < ActiveRecord::Base + scope :published, where(:published => true).joins(:category) + end + + +Scopes are also chainable within scopes: + + + class Post < ActiveRecord::Base + scope :published, where(:published => true) + scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0)) + end + + +To call this +published+ scope we can call it on either the class: + + + Post.published => [published posts] + + +Or on an association consisting of +Post+ objects: + + + category = Category.first + category.posts.published => [published posts, belonging to this category] + + h3. Dynamic Finders For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +first_name+ on your +Client+ model for example, you get +find_by_first_name+ and +find_all_by_first_name+ for free from Active Record. If you have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+ methods. -- cgit v1.2.3 From 5b19579ab3a2ea95f8354cc6aa407bf2e5264d26 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:35:59 +1000 Subject: Init guide: further revision, covering rails/plugin.rb and friends --- railties/guides/source/initialization.textile | 120 +++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 4 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index 7c6b3b7912..1504b973a5 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -222,9 +222,17 @@ h4. +actionpack/lib/action_dispatch.rb+ Action Dispatch is the routing component of the Rails framework. It depends on Active Support, +actionpack/lib/action_pack.rb+ and +Rack+ being available. The first thing required here is +active_support+. -h4. +active_support/lib/active_support.rb+ +h4. +activesupport/lib/active_support.rb+ -This file begins with requiring +active_support/lib/active_support/dependencies/autoload.rb+ which redefines Ruby's +autoload+ method to have a little more extra behaviour especially in regards to eager autoloading. Eager autoloading is the loading of all required classes and will happen when the +config.cache_classes+ setting is +true+. +This file begins with requiring +active_support/lib/active_support/dependencies/autoload.rb+ which redefines Ruby's +autoload+ method to have a little more extra behaviour especially in regards to eager autoloading. Eager autoloading is the loading of all required classes and will happen when the +config.cache_classes+ setting is +true+. + +In this file there are a lot of lines such as this inside the +ActiveSupport+ module: + + + autoload :Inflector + + +Due to the overriding of the +autoload+ method, Ruby will know to look for this file at +activesupport/lib/active_support/inflector.rb+ when the +Inflector+ class is first referenced. The +active_support/lib/active_support/version.rb+ that is also required here simply defines an +ActiveSupport::VERSION+ constant which defines a couple of constants inside this module, the main constant of this is +ActiveSupport::VERSION::STRING+ which returns the current version of ActiveSupport. @@ -450,7 +458,11 @@ h4. +config/application.rb+ This file requires +config/boot.rb+, but only if it hasn't been required before, which would be the case in +rails server+ but *wouldn't* be the case with Passenger. -Then the fun begins! The next line is: +Then the fun begins! + +h3. Loading Rails + +The next line in +config/application.rb+ is: require 'rails/all' @@ -599,10 +611,110 @@ This file is the next file required from +rails/configuration.rb+ is the file th The next file required is +active_support/core_ext/hash/deep_dup+ which is covered in "Active Support Core Extensions guide":http://guides.rubyonrails.org/active_support_core_extensions.html#deep_dup -The file after that is +rails/paths+ +The file that is required next from is +rails/paths+ h4. +railties/lib/rails/paths.rb+ +This file defines the +Rails::Paths+ module which allows paths to be configured for a Rails application or engine. Later on in this guide when we cover Rails configuration during the initialization process we'll see this used to set up some default paths for Rails and some of them will be configured to be eager loaded. + +h4. +railties/lib/rails/rack.rb+ + +The final file to be loaded by +railties/lib/rails/configuration.rb+ is +rails/rack+ which defines some simple autoloads: + + + module Rails + module Rack + autoload :Debugger, "rails/rack/debugger" + autoload :Logger, "rails/rack/logger" + autoload :LogTailer, "rails/rack/log_tailer" + autoload :Static, "rails/rack/static" + end + end + + +Once this file is finished loading, then the +Rails::Configuration+ class is initialized. This completes the loading of +railties/lib/rails/configuration.rb+ and now we jump back to the loading of +railties/lib/rails/railtie.rb+, where the next file loaded is +active_support/inflector+. + +h4. +activesupport/lib/active_support/inflector.rb+ + ++active_support/inflector.rb+ requires a series of file which are responsible for setting up the basics for knowing how to pluralize and singularize words. These files are: + + + require 'active_support/inflector/inflections' + require 'active_support/inflector/transliterate' + require 'active_support/inflector/methods' + + require 'active_support/inflections' + require 'active_support/core_ext/string/inflections' + + +h4. +activesupport/lib/active_support/inflector/inflections.rb+ + +This file references the +ActiveSupport::Inflector+ constant which isn't loaded by this point. But there were autoloads set up in +activesupport/lib/active_support.rb+ which will load the file which loads this constant and so then it will be defined. Then this file defines pluralization and singularization rules for words in Rails. This is how Rails knows how to pluralize "tomato" to "tomatoes". + +h4. +activesupport/lib/active_support/inflector/transliterate.rb+ + +In this file is where the "+transliterate+":http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate and +parameterize+:http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize methods are defined. The documentation for both of these methods is very much worth reading. + +h4. +activesupport/lib/active_support/inflector/methods.rb+ + +The +methods.rb+ file is responsible for defining methods such as +camelize+, +underscore+ and +dasherize+ as well as a slew of others. The "+ActiveSupport::Inflector+ documentation":http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html covers them all pretty decently. + +h4. Back to +railties/lib/rails/railtie.rb+ + +Once the inflector files have been loaded, the +Rails::Railtie+ class is defined. This class includes a module called +Initializable+, which is actually +Rails::Initializable+ and is automatically loaded at this point. + +h4. +railties/lib/rails/initializable.rb+ + +When the module from this file (+Rails::Initializable+) is included, it extends the class it's included into with the +ClassMethods+ module inside of it. This module defines the +initializer+ method which is used to define initializers throughout all of the railties. This file completes the loading of +railties/lib/rails/railtie.rb+. Now we go back to +rails/engine.rb+. + +h4. +railties/lib/rails/engine.rb+ + +The next file required in +rails/engine.rb+ is +active_support/core_ext/module/delegation+ which is documented in the "Active Support Core Extensions Guide":http://guides.rubyonrails.org/active_support_core_extensions.html#method-delegation. + +The next two files after this are Ruby standard library files: +pathname+ and +rbconfig+. The file after these is +rails/engine/railties+. + +h4. +railties/lib/rails/engine/railties.rb+ + +This file defines the +Rails::Engine::Railties+ class which provides the +engines+ and +railties+ methods which are used later on for defining rake tasks and other functionality for engines and railties. + +h4. Back to +railties/lib/rails/engine.rb+ + +Once +rails/engine/railties.rb+ has finished loading the +Rails::Engine+ class gets its basic functionality defined, such as the +inherited+ method which will be called when this class is inherited from. + +Once this file has finished loading we jump back to +railties/lib/rails/plugin.rb+ + +h4. Back to +railties/lib/rails/plugin.rb+ + +The next file required in this is a core extension from Active Support called +array/conversions+ which is covered in "this section":http://guides.rubyonrails.org/active_support_core_extensions.html#array-conversions of the Active Support Core Extensions Guide. + +Once that file has finished loading, the +Rails::Plugin+ class is defined. + +h4. Back to +railties/lib/rails/application.rb+ + +Jumping back to +rails/application.rb+ now. This file defines the +Rails::Application+ class where the application's class inherits from. This class (and its superclasses) define the basic behaviour on the application's constant such as the +config+ method used for configuring the application. + +Once this file's done then we go back to the +railties/lib/rails.rb+ file, which next requires +rails/version+. + +h4. +railties/lib/rails/version.rb+ + +Much like +active_support/version+, this file defines the +VERSION+ constant which has a +STRING+ constant on it which returns the current version of Rails. + +h4. +activesupport/lib/active_support/railtie.rb+ + +This file requires +active_support+ and +rails+ which have already been required so these two lines are effectively ignored. The third require in this file is to +active_support/railtie+. + +h4. +activesupport/lib/active_support/i18n_railtie.rb+ + +This file is the first file that sets up configuration with these lines inside the class: + + + class Railtie < Rails::Railtie + config.i18n = ActiveSupport::OrderedOptions.new + config.i18n.railties_load_path = [] + config.i18n.load_path = [] + config.i18n.fallbacks = ActiveSupport::OrderedOptions.new + + -- cgit v1.2.3 From 28b1642561cddc1f8650f7a8c740ece259db913b Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:50:40 +1000 Subject: Query guide: lambdas must be used when working with scopes --- railties/guides/source/active_record_querying.textile | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index fe6d284239..25f3723b93 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -763,6 +763,18 @@ Or on an association consisting of +Post+ objects: category.posts.published => [published posts, belonging to this category] +h4. Working with times + +If you're working with dates or times within scopes, due to how they are evaluated, you will need to use a lambda so that the scope is evaluated every time. + + + class Post < ActiveRecord::Base + scope :last_week, lambda { where("created_at < ?", Time.zone.now ) } + end + + +Without the +lambda+, this +Time.zone.now+ will only be called once. + h3. Dynamic Finders For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +first_name+ on your +Client+ model for example, you get +find_by_first_name+ and +find_all_by_first_name+ for free from Active Record. If you have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+ methods. -- cgit v1.2.3 From f411451ed513d982a2a14e62c38f030a3560ea42 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:53:26 +1000 Subject: Remove comma [samuelkadoph] --- railties/guides/source/active_record_querying.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 25f3723b93..6c1042e70e 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -760,7 +760,7 @@ Or on an association consisting of +Post+ objects: category = Category.first - category.posts.published => [published posts, belonging to this category] + category.posts.published => [published posts belonging to this category] h4. Working with times -- cgit v1.2.3 From b4b2574a12f7a3b6db97d28eda894ad872445190 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:55:42 +1000 Subject: Query guide: fix indentation --- .../guides/source/active_record_querying.textile | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 6c1042e70e..5cd302f356 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -728,39 +728,39 @@ Scoping allows you to specify commonly-used ARel queries which can be referenced To define a simple scope, we use the +scope+ method inside the class, passing the ARel query that we'd like run when this scope is called: - class Post < ActiveRecord::Base - scope :published, where(:published => true) - end +class Post < ActiveRecord::Base + scope :published, where(:published => true) +end Just like before, these methods are also chainable: - class Post < ActiveRecord::Base - scope :published, where(:published => true).joins(:category) - end +class Post < ActiveRecord::Base + scope :published, where(:published => true).joins(:category) +end Scopes are also chainable within scopes: - class Post < ActiveRecord::Base - scope :published, where(:published => true) - scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0)) - end +class Post < ActiveRecord::Base + scope :published, where(:published => true) + scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0)) +end To call this +published+ scope we can call it on either the class: - Post.published => [published posts] +Post.published => [published posts] Or on an association consisting of +Post+ objects: - category = Category.first - category.posts.published => [published posts belonging to this category] +category = Category.first +category.posts.published => [published posts belonging to this category] h4. Working with times -- cgit v1.2.3 From 0644e38c4269c06968cacd60ac677011af82671c Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:57:53 +1000 Subject: Query guide: scopes with arguments should be instead defined as class methods. --- .../guides/source/active_record_querying.textile | 34 ++++++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 5cd302f356..eb77a35739 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -768,13 +768,41 @@ h4. Working with times If you're working with dates or times within scopes, due to how they are evaluated, you will need to use a lambda so that the scope is evaluated every time. - class Post < ActiveRecord::Base - scope :last_week, lambda { where("created_at < ?", Time.zone.now ) } - end +class Post < ActiveRecord::Base + scope :last_week, lambda { where("created_at < ?", Time.zone.now ) } +end Without the +lambda+, this +Time.zone.now+ will only be called once. +h4. Passing in arguments + +When a +lambda+ is used for a +scope+, it can take arguments: + + +class Post < ActiveRecord::Base + scope :1_week_before, lambda { |time| where("created_at < ?", time) +end + + +This may then be called using this: + + +Post.1_week_before(Time.zone.now) + + +However, this is just duplicating the functionality that would be provided to you by a class method. + + +class Post < ActiveRecord::Base + def self.1_week_before(time) + where("created_at < ?", time) + end +end + + +Using a class method is the preferred way to accept arguments for scopes. + h3. Dynamic Finders For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +first_name+ on your +Client+ model for example, you get +find_by_first_name+ and +find_all_by_first_name+ for free from Active Record. If you have a +locked+ field on the +Client+ model, you also get +find_by_locked+ and +find_all_by_locked+ methods. -- cgit v1.2.3 From 226a2a90edeaaa31948c05a6633c9edbe71fb636 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:58:33 +1000 Subject: Query guide: class methods are still available on associations --- railties/guides/source/active_record_querying.textile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index eb77a35739..bdc4d7c50d 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -801,7 +801,11 @@ class Post < ActiveRecord::Base end -Using a class method is the preferred way to accept arguments for scopes. +Using a class method is the preferred way to accept arguments for scopes. These methods will still be accessible on the association objects: + + +category.posts.1_week_before(time) + h3. Dynamic Finders -- cgit v1.2.3 From c3748190f45a7a48c68606cb2c44a246f978c6ac Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:59:22 +1000 Subject: Query Guide: update Changelog --- railties/guides/source/active_record_querying.textile | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index bdc4d7c50d..c9c884cd7c 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -968,6 +968,7 @@ For options, please see the parent section, "Calculations":#calculations. h3. Changelog +* December 23 2010: Add documentation for the +scope+ method. "Ryan Bigg":http://ryanbigg.com * April 7, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com * February 3, 2010: Update to Rails 3 by "James Miller":credits.html#bensie * February 7, 2009: Second version by "Pratik":credits.html#lifo -- cgit v1.2.3 From 0795c3a15eccf5406220134fae3f4b44be030655 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 15:05:17 +1000 Subject: Query guide: Ordering, selecting and so on shouldn't be nested in conditions as they are modifiers, not conditions in the prime sense of the word. --- railties/guides/source/active_record_querying.textile | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index c9c884cd7c..b315a2b107 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -337,7 +337,7 @@ This code will generate SQL like this: SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5)) -h4. Ordering +h3. Ordering To retrieve records from the database in a specific order, you can use the +order+ method. @@ -361,7 +361,7 @@ Or ordering by multiple fields: Client.order("orders_count ASC, created_at DESC") -h4. Selecting Specific Fields +h3. Selecting Specific Fields By default, Model.find selects all the fields from the result set using +select *+. @@ -397,7 +397,7 @@ You can also call SQL functions within the select option. For example, if you wo Client.select("DISTINCT(name)") -h4. Limit and Offset +h3. Limit and Offset To apply +LIMIT+ to the SQL fired by the +Model.find+, you can specify the +LIMIT+ using +limit+ and +offset+ methods on the relation. @@ -425,7 +425,7 @@ will return instead a maximum of 5 clients beginning with the 31st. The SQL look SELECT * FROM clients LIMIT 5, 30 -h4. Group +h3. Group To apply a +GROUP BY+ clause to the SQL fired by the finder, you can specify the +group+ method on the find. @@ -443,7 +443,7 @@ The SQL that would be executed would be something like this: SELECT * FROM orders GROUP BY date(created_at) -h4. Having +h3. Having SQL uses the +HAVING+ clause to specify conditions on the +GROUP BY+ fields. You can add the +HAVING+ clause to the SQL fired by the +Model.find+ by adding the +:having+ option to the find. @@ -461,7 +461,7 @@ SELECT * FROM orders GROUP BY date(created_at) HAVING created_at > '2009-01-15' This will return single order objects for each day, but only for the last month. -h4. Readonly Objects +h3. Readonly Objects Active Record provides +readonly+ method on a relation to explicitly disallow modification or deletion of any of the returned object. Any attempt to alter or destroy a readonly record will not succeed, raising an +ActiveRecord::ReadOnlyRecord+ exception. @@ -473,7 +473,7 @@ client.save As +client+ is explicitly set to be a readonly object, the above code will raise an +ActiveRecord::ReadOnlyRecord+ exception when calling +client.save+ with an updated value of _visists_. -h4. Locking Records for Update +h3. Locking Records for Update Locking is helpful for preventing race conditions when updating records in the database and ensuring atomic updates. @@ -482,7 +482,7 @@ Active Record provides two locking mechanisms: * Optimistic Locking * Pessimistic Locking -h5. Optimistic Locking +h4. Optimistic Locking Optimistic locking allows multiple users to access the same record for edits, and assumes a minimum of conflicts with the data. It does this by checking whether another process has made changes to a record since it was opened. An +ActiveRecord::StaleObjectError+ exception is thrown if that has occurred and the update is ignored. @@ -517,7 +517,7 @@ class Client < ActiveRecord::Base end -h5. Pessimistic Locking +h4. Pessimistic Locking Pessimistic locking uses a locking mechanism provided by the underlying database. Using +lock+ when building a relation obtains an exclusive lock on the selected rows. Relations using +lock+ are usually wrapped inside a transaction for preventing deadlock conditions. -- cgit v1.2.3 From cef17cd705c3529139fa1fd5bbc0e9c6ae289ba4 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 15:44:12 +1000 Subject: Query guide: specify what type of objects scopes return --- railties/guides/source/active_record_querying.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index b315a2b107..5264477e1c 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -723,7 +723,7 @@ Even though Active Record lets you specify conditions on the eager loaded associ h3. Scopes -Scoping allows you to specify commonly-used ARel queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as +where+, +joins+ and +includes+. +Scoping allows you to specify commonly-used ARel queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as +where+, +joins+ and +includes+. All scope methods will return an +ActiveRecord::Relation+ object which will allow for further methods (such as other scopes) to be called on it. To define a simple scope, we use the +scope+ method inside the class, passing the ARel query that we'd like run when this scope is called: -- cgit v1.2.3 From 578f9711fdb42ca9fc4b8248c494afe755cd1c17 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 16:06:29 +1000 Subject: Query guide: arel_table, eq and eq_any --- .../guides/source/active_record_querying.textile | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 5264477e1c..1da12cbc9b 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -337,6 +337,32 @@ This code will generate SQL like this: SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5)) +h4. +arel_table+ + +The +arel_table+ method is used in the following sections to gain access to more advanced functionality of the Active Relation building language. You may see examples such as this: + + +Post.where(Post.arel_table[:comments_count].eq(5))) + + +Don't be alarmed! This method returns an +Arel::Table+ object which provides methods for accessing fields and building queries, as you'll see in the following sections. + +h4. +eq+ + +The +eq+ method can be used to check if a field is a specific value: + + +Post.where(Post.arel_table[:comments_count].eq(5)) + + +h4. +eq_any+ + +The +eq_any+ method can be used to return objects of a relation that have the specified field matching any of the specified values. + + +Post.where(Post.arel_table[:comments_count].eq_any([1,2]) + + h3. Ordering To retrieve records from the database in a specific order, you can use the +order+ method. -- cgit v1.2.3 From 6271f3f16b5b9c6d60ba7d758d2b595be8074e00 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 16:39:10 +1000 Subject: Query guide: Document "in" methods --- .../guides/source/active_record_querying.textile | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 1da12cbc9b..1bd9ec7972 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -363,6 +363,31 @@ The +eq_any+ method can be used to return objects of a relation that have the sp Post.where(Post.arel_table[:comments_count].eq_any([1,2]) +h4. +in+ + +To check if a value is within a given group of values, use the +in+ method: + + + Post.where(Post.arel_table[:id].in([1,2,3])) + + +h4. +in_any+ + +Check if a value is within any one of a group of values: + + + Post.where(Post.arel_table[:id]).in_any([1,2,3], [4,5,6]) + + +h4. +in_all+ + +Check if a value is within all of the specified groups of values: + + + Post.where(Post.arel_table[:id]).in_all([1,2,3], [1,4,6]) + + + h3. Ordering To retrieve records from the database in a specific order, you can use the +order+ method. -- cgit v1.2.3 From 8713bb6ab8ff5acf60872b0b8f0bffd13a0aa1ca Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 18:36:39 +1000 Subject: Query guide: cover further ARel methods --- .../guides/source/active_record_querying.textile | 83 ++++++++++++++++++++-- 1 file changed, 76 insertions(+), 7 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 1bd9ec7972..8eb28ec953 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -355,38 +355,107 @@ The +eq+ method can be used to check if a field is a specific value: Post.where(Post.arel_table[:comments_count].eq(5)) +This method's opposite is +not_eq+. + h4. +eq_any+ -The +eq_any+ method can be used to return objects of a relation that have the specified field matching any of the specified values. +Checks if the specified field matches any of the given values. Post.where(Post.arel_table[:comments_count].eq_any([1,2]) +This method's opposite is +not_eq_any+. + h4. +in+ -To check if a value is within a given group of values, use the +in+ method: +To check if a field is within a given group of values, use the +in+ method: - Post.where(Post.arel_table[:id].in([1,2,3])) +Post.where(Post.arel_table[:id].in([1,2,3])) +This method's opposite is +not_in+. + h4. +in_any+ -Check if a value is within any one of a group of values: +Check if a field is within any one of a group of values: - Post.where(Post.arel_table[:id]).in_any([1,2,3], [4,5,6]) +Post.where(Post.arel_table[:id]).in_any([1,2,3], [4,5,6]) +This method's opposite is +not_in_any+. + h4. +in_all+ -Check if a value is within all of the specified groups of values: +Check if a field is within all of the specified groups of values: + + +Post.where(Post.arel_table[:id]).in_all([1,2,3], [1,4,6]) + + +This method's opposite is +not_in_all+. + +h4. +matches+ + +Match a specific field with a given value. Use +%+ for wildcard searching. + + +Post.where(Post.arel_table[:author].matches("Ryan%")) + + +This method's opposite is +does_not_match+. + +h4. +matches_any+ + +Match a specific field with any given value. Use +%+ for wildcard searching. + + +Post.where(Post.arel_table[:author].matches_any(["Ryan%", "Yehuda%"])) + + +This method's opposite is +does_not_match_any+ + +h4. +matches_all+ + +Match a specific field with all given values. Use +%+ for wild card searching. + + +Post.where(Post.arel_table[:author].matches_all(["Ryan%", "%Bigg"])) + + +This method's opposite is +does_not_match_all+. + +h4. +gteq+ + +Check for a field greater than or equal to a specific value. + + +Post.where(Post.arel_table[:comments_count].gteq(1)) + + +This method's opposite is +lteq+. + +h4. +gteq_any+ + +Check for a field greater than or equal to any of the given values. + + +Post.where(Post.arel_table[:comments_count].gteq_any([1,2])) + + +This method's opposite is +lteq_any+. + +h4. +gteq_all+ + +Check for a field greater than or equal to all of the given values. - Post.where(Post.arel_table[:id]).in_all([1,2,3], [1,4,6]) +Post.where(Post.arel_table[:comments_count].gteq_all([1,2])) +This method's opposite is +lteq_all+. h3. Ordering -- cgit v1.2.3 From 890105f4f1ca471491c85f2aabd21d5a183ab189 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 22:15:08 +1000 Subject: Query guide: add or conditions --- railties/guides/source/active_record_querying.textile | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 8eb28ec953..b4db365a09 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -457,6 +457,17 @@ Post.where(Post.arel_table[:comments_count].gteq_all([1,2])) This method's opposite is +lteq_all+. +h4. +or+ + +Allows you to chain queries to get results matching either condition: + + +title = Post.arel_table[:title] +Post.where(title.eq("Active").or(title.eq("Record"))) + + +Note that this method is called on the end of the +eq+ method here, rather than the +where+. + h3. Ordering To retrieve records from the database in a specific order, you can use the +order+ method. -- cgit v1.2.3 From bb707cf7373586952d95bf574cfdeb2dbac29ea2 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Thu, 23 Dec 2010 23:34:28 +0530 Subject: indentation fix --- railties/guides/source/3_0_release_notes.textile | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/3_0_release_notes.textile b/railties/guides/source/3_0_release_notes.textile index adb1c755df..db5a9ce644 100644 --- a/railties/guides/source/3_0_release_notes.textile +++ b/railties/guides/source/3_0_release_notes.textile @@ -357,15 +357,15 @@ Validations have been moved from Active Record into Active Model, providing an i * There is now a validates :attribute, options_hash shortcut method that allows you to pass options for all the validates class methods, you can pass more than one option to a validate method. * The +validates+ method has the following options: - * :acceptance => Boolean. - * :confirmation => Boolean. - * :exclusion => { :in => Enumerable }. - * :inclusion => { :in => Enumerable }. - * :format => { :with => Regexp, :on => :create }. - * :length => { :maximum => Fixnum }. - * :numericality => Boolean. - * :presence => Boolean. - * :uniqueness => Boolean. +** :acceptance => Boolean. +** :confirmation => Boolean. +** :exclusion => { :in => Enumerable }. +** :inclusion => { :in => Enumerable }. +** :format => { :with => Regexp, :on => :create }. +** :length => { :maximum => Fixnum }. +** :numericality => Boolean. +** :presence => Boolean. +** :uniqueness => Boolean. NOTE: All the Rails version 2.3 style validation methods are still supported in Rails 3.0, the new validates method is designed as an additional aid in your model validations, not a replacement for the existing API. -- cgit v1.2.3 From 2c8938fcba6670f2cb056ec91d631ae0e37ea006 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Fri, 24 Dec 2010 00:03:51 +0530 Subject: fixed inject example and some minor edits --- .../guides/source/active_support_core_extensions.textile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index bddff1c987..8821a6e461 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -1568,7 +1568,7 @@ The method +tableize+ is +underscore+ followed by +pluralize+. "InvoiceLine".tableize # => "invoice_lines" -As a rule of thumb, +tableize+ returns the table name that corresponds to a given model for simple cases. The actual implementation in Active Record is not straight +tableize+ indeed, because it also demodulizes de class name and checks a few options that may affect the returned string. +As a rule of thumb, +tableize+ returns the table name that corresponds to a given model for simple cases. The actual implementation in Active Record is not straight +tableize+ indeed, because it also demodulizes the class name and checks a few options that may affect the returned string. NOTE: Defined in +active_support/core_ext/string/inflections.rb+. @@ -1868,7 +1868,7 @@ The sum of an empty collection is zero by default, but this is customizable: [].sum(1) # => 1 -If a block is given +sum+ becomes an iterator that yields the elements of the collection and sums the returned values: +If a block is given, +sum+ becomes an iterator that yields the elements of the collection and sums the returned values: (1..5).sum {|n| n * 2 } # => 30 @@ -1896,8 +1896,8 @@ h4. +each_with_object+ The +inject+ method offers iteration with an accumulator: -[2, 3, 4].inject(1) {|acc, i| product*i } # => 24 - +[2, 3, 4].inject(1) {|product, i| product*i } # => 24 + 1+: <% end %> -If an optional block is given +many?+ only takes into account those elements that return true: +If an optional block is given, +many?+ only takes into account those elements that return true: @see_more = videos.many? {|video| video.category == params[:category]} @@ -1952,7 +1952,7 @@ NOTE: Defined in +active_support/core_ext/enumerable.rb+. h4. +exclude?+ -The predicate +exclude?+ tests whether a given object does *not* belong to the collection. It is the negation of the builtin +include?+: +The predicate +exclude?+ tests whether a given object does *not* belong to the collection. It is the negation of the built-in +include?+: to_visit << node if visited.exclude?(node) -- cgit v1.2.3 From 5683d6883b20e5ed88e26d07c841911b225a5841 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Fri, 24 Dec 2010 00:17:44 +0530 Subject: It's JavaScript :) --- railties/guides/source/2_2_release_notes.textile | 6 +++--- railties/guides/source/2_3_release_notes.textile | 2 +- .../guides/source/action_view_overview.textile | 22 +++++++++++----------- railties/guides/source/getting_started.textile | 2 +- .../guides/source/layouts_and_rendering.textile | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/2_2_release_notes.textile b/railties/guides/source/2_2_release_notes.textile index 5628d7e52f..8e2d528eee 100644 --- a/railties/guides/source/2_2_release_notes.textile +++ b/railties/guides/source/2_2_release_notes.textile @@ -260,15 +260,15 @@ h4. Other Action Controller Changes * Benchmarking numbers are now reported in milliseconds rather than tiny fractions of seconds * Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers. * +redirect_to+ now fully supports URI schemes (so, for example, you can redirect to a svn+ssh: URI). -* +render+ now supports a +:js+ option to render plain vanilla javascript with the right mime type. +* +render+ now supports a +:js+ option to render plain vanilla JavaScript with the right mime type. * Request forgery protection has been tightened up to apply to HTML-formatted content requests only. * Polymorphic URLs behave more sensibly if a passed parameter is nil. For example, calling +polymorphic_path([@project, @date, @area])+ with a nil date will give you +project_area_path+. h3. Action View * +javascript_include_tag+ and +stylesheet_link_tag+ support a new +:recursive+ option to be used along with +:all+, so that you can load an entire tree of files with a single line of code. -* The included Prototype javascript library has been upgraded to version 1.6.0.3. -* +RJS#page.reload+ to reload the browser's current location via javascript +* The included Prototype JavaScript library has been upgraded to version 1.6.0.3. +* +RJS#page.reload+ to reload the browser's current location via JavaScript * The +atom_feed+ helper now takes an +:instruct+ option to let you insert XML processing instructions. h3. Action Mailer diff --git a/railties/guides/source/2_3_release_notes.textile b/railties/guides/source/2_3_release_notes.textile index 72fe723687..67743a4797 100644 --- a/railties/guides/source/2_3_release_notes.textile +++ b/railties/guides/source/2_3_release_notes.textile @@ -278,7 +278,7 @@ Mime::JS =~ "text/javascript" => true Mime::JS =~ "application/javascript" => true -The other change is that the framework now uses the +Mime::JS+ when checking for javascript in various spots, making it handle those alternatives cleanly. +The other change is that the framework now uses the +Mime::JS+ when checking for JavaScript in various spots, making it handle those alternatives cleanly. * Lead Contributor: "Seth Fitzsimmons":http://www.workingwithrails.com/person/5510-seth-fitzsimmons diff --git a/railties/guides/source/action_view_overview.textile b/railties/guides/source/action_view_overview.textile index 051baa660c..30faeeaa91 100644 --- a/railties/guides/source/action_view_overview.textile +++ b/railties/guides/source/action_view_overview.textile @@ -231,7 +231,7 @@ input("post", "title") # => h4. AssetTagHelper -This module provides methods for generating HTML that links views to assets such as images, javascripts, stylesheets, and feeds. +This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds. By default, Rails links to these assets on the current host in the public folder, but you can direct Rails to link to assets from a dedicated assets server by setting +ActionController::Base.asset_host+ in the application configuration, typically in +config/environments/production.rb+. For example, let's say your asset host is +assets.example.com+: @@ -242,7 +242,7 @@ image_tag("rails.png") # => "RSS h5. image_path -Computes the path to an image asset in the public images directory. Full paths from the document root will be passed through. Used internally by +image_tag+ to build the image path. +Computes the path to an image asset in the +public/images+ directory. Full paths from the document root will be passed through. Used internally by +image_tag+ to build the image path. image_path("edit.png") # => /images/edit.png @@ -286,7 +286,7 @@ image_path("edit.png") # => /images/edit.png h5. image_tag -Returns an html image tag for the source. The source can be a full path or a file that exists in your public images directory. +Returns an html image tag for the source. The source can be a full path or a file that exists in your +public/images+ directory. image_tag("icon.png") # => Icon @@ -294,26 +294,26 @@ image_tag("icon.png") # => Icon h5. javascript_include_tag -Returns an html script tag for each of the sources provided. You can pass in the filename (+.js+ extension is optional) of javascript files that exist in your +public/javascripts+ directory for inclusion into the current page or you can pass the full path relative to your document root. +Returns an html script tag for each of the sources provided. You can pass in the filename (+.js+ extension is optional) of JavaScript files that exist in your +public/javascripts+ directory for inclusion into the current page or you can pass the full path relative to your document root. javascript_include_tag "common" # => -To include the Prototype and Scriptaculous javascript libraries in your application, pass +:defaults+ as the source. When using +:defaults+, if an +application.js+ file exists in your +public/javascripts+ directory, it will be included as well. +To include the Prototype and Scriptaculous JavaScript libraries in your application, pass +:defaults+ as the source. When using +:defaults+, if an +application.js+ file exists in your +public/javascripts+ directory, it will be included as well. javascript_include_tag :defaults -You can also include all javascripts in the javascripts directory using +:all+ as the source. +You can also include all JavaScript files in the +public/javascripts+ directory using +:all+ as the source. javascript_include_tag :all -You can also cache multiple javascripts into one file, which requires less HTTP connections to download and can better be compressed by gzip (leading to faster transfers). Caching will only happen if +ActionController::Base.perform_caching+ is set to true (which is the case by default for the Rails production environment, but not for the development environment). +You can also cache multiple JavaScript files into one file, which requires less HTTP connections to download and can better be compressed by gzip (leading to faster transfers). Caching will only happen if +ActionController::Base.perform_caching+ is set to true (which is the case by default for the Rails production environment, but not for the development environment). javascript_include_tag :all, :cache => true # => @@ -322,7 +322,7 @@ javascript_include_tag :all, :cache => true # => h5. javascript_path -Computes the path to a javascript asset in the +public/javascripts+ directory. If the source filename has no extension, +.js+ will be appended. Full paths from the document root will be passed through. Used internally by +javascript_include_tag+ to build the script path. +Computes the path to a JavaScript asset in the +public/javascripts+ directory. If the source filename has no extension, +.js+ will be appended. Full paths from the document root will be passed through. Used internally by +javascript_include_tag+ to build the script path. javascript_path "common" # => /javascripts/common.js @@ -352,7 +352,7 @@ stylesheet_link_tag :all, :cache => true h5. stylesheet_path -Computes the path to a stylesheet asset in the public stylesheets directory. If the source filename has no extension, .css will be appended. Full paths from the document root will be passed through. Used internally by stylesheet_link_tag to build the stylesheet path. +Computes the path to a stylesheet asset in the +public/stylesheets+ directory. If the source filename has no extension, .css will be appended. Full paths from the document root will be passed through. Used internally by stylesheet_link_tag to build the stylesheet path. stylesheet_path "application" # => /stylesheets/application.css @@ -1076,7 +1076,7 @@ h4. JavaScriptHelper Provides functionality for working with JavaScript in your views. -Rails includes the Prototype JavaScript framework and the Scriptaculous JavaScript controls and visual effects library. If you wish to use these libraries and their helpers, make sure +<%= javascript_include_tag :defaults, :cache => true %>+ is in the HEAD section of your page. This function will include the necessary JavaScript files Rails generated in the public/javascripts directory. +Rails includes the Prototype JavaScript framework and the Scriptaculous JavaScript controls and visual effects library. If you wish to use these libraries and their helpers, make sure +<%= javascript_include_tag :defaults, :cache => true %>+ is in the HEAD section of your page. This function will include the necessary JavaScript files Rails generated in the +public/javascripts+ directory. h5. button_to_function diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index e371632d87..ef93239b30 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -184,7 +184,7 @@ In any case, Rails will create a folder in your working directory called blo |doc/|In-depth documentation for your application.| |lib/|Extended modules for your application (not covered in this guide).| |log/|Application log files.| -|public/|The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.| +|public/|The only folder seen to the world as-is. This is where your images, JavaScript files, stylesheets (CSS), and other static files go.| |script/|Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.| |test/|Unit tests, fixtures, and other test apparatus. These are covered in "Testing Rails Applications":testing.html| |tmp/|Temporary files| diff --git a/railties/guides/source/layouts_and_rendering.textile b/railties/guides/source/layouts_and_rendering.textile index 4cdd4f2709..f0e40b0980 100644 --- a/railties/guides/source/layouts_and_rendering.textile +++ b/railties/guides/source/layouts_and_rendering.textile @@ -681,7 +681,7 @@ There are three tag options available for +auto_discovery_link_tag+: * +:type+ specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically. * +:title+ specifies the title of the link -h5. Linking to Javascript Files with +javascript_include_tag+ +h5. Linking to JavaScript Files with +javascript_include_tag+ The +javascript_include_tag+ helper returns an HTML +script+ tag for each source provided. Rails looks in +public/javascripts+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/javascripts/main.js+: -- cgit v1.2.3 From f1eb69c5b27050494cbc91890b107abd1dadfd48 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Fri, 24 Dec 2010 00:24:26 +0530 Subject: fixing typo that crept in 2c8938f --- railties/guides/source/active_support_core_extensions.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 8821a6e461..33e281d59b 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -1897,7 +1897,7 @@ The +inject+ method offers iteration with an accumulator: [2, 3, 4].inject(1) {|product, i| product*i } # => 24 - The block is expected to return the value for the accumulator in the next iteration, and this makes building mutable objects a bit cumbersome: -- cgit v1.2.3 From 06e2f7cd173a279a4b1c17040356acca142128b5 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Fri, 24 Dec 2010 00:34:07 +0530 Subject: minor convention edit --- railties/guides/source/getting_started.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/guides') diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index ef93239b30..4466c291bb 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -227,7 +227,7 @@ NOTE: In this guide we are using an SQLite3 database for data storage, because i h5. Configuring a MySQL Database -If you choose to use MySQL instead of the shipped Sqlite3 database, your +config/database.yml+ will look a little different. Here's the development section: +If you choose to use MySQL instead of the shipped SQLite3 database, your +config/database.yml+ will look a little different. Here's the development section: development: -- cgit v1.2.3 From e4314e7d3fd93c5ed1eda1cf52ed38cdc2543d25 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Fri, 24 Dec 2010 07:22:08 +1000 Subject: Query guide: visists => visits --- railties/guides/source/active_record_querying.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index b4db365a09..a45624809e 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -602,7 +602,7 @@ client.visits += 1 client.save -As +client+ is explicitly set to be a readonly object, the above code will raise an +ActiveRecord::ReadOnlyRecord+ exception when calling +client.save+ with an updated value of _visists_. +As +client+ is explicitly set to be a readonly object, the above code will raise an +ActiveRecord::ReadOnlyRecord+ exception when calling +client.save+ with an updated value of _visits_. h3. Locking Records for Update -- cgit v1.2.3 From d1e95e12b4d56fac58ee680fd2d8d5cdac57b2ac Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Fri, 24 Dec 2010 10:47:38 +1000 Subject: Revert "Query guide: arel_table, eq and eq_any" along with other commits that added documentation involving the arel_table method This reverts commit 578f9711fdb42ca9fc4b8248c494afe755cd1c17. Conflicts: railties/guides/source/active_record_querying.textile --- .../guides/source/active_record_querying.textile | 131 --------------------- 1 file changed, 131 deletions(-) (limited to 'railties/guides') diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index a45624809e..61d8205278 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -337,137 +337,6 @@ This code will generate SQL like this: SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5)) -h4. +arel_table+ - -The +arel_table+ method is used in the following sections to gain access to more advanced functionality of the Active Relation building language. You may see examples such as this: - - -Post.where(Post.arel_table[:comments_count].eq(5))) - - -Don't be alarmed! This method returns an +Arel::Table+ object which provides methods for accessing fields and building queries, as you'll see in the following sections. - -h4. +eq+ - -The +eq+ method can be used to check if a field is a specific value: - - -Post.where(Post.arel_table[:comments_count].eq(5)) - - -This method's opposite is +not_eq+. - -h4. +eq_any+ - -Checks if the specified field matches any of the given values. - - -Post.where(Post.arel_table[:comments_count].eq_any([1,2]) - - -This method's opposite is +not_eq_any+. - -h4. +in+ - -To check if a field is within a given group of values, use the +in+ method: - - -Post.where(Post.arel_table[:id].in([1,2,3])) - - -This method's opposite is +not_in+. - -h4. +in_any+ - -Check if a field is within any one of a group of values: - - -Post.where(Post.arel_table[:id]).in_any([1,2,3], [4,5,6]) - - -This method's opposite is +not_in_any+. - -h4. +in_all+ - -Check if a field is within all of the specified groups of values: - - -Post.where(Post.arel_table[:id]).in_all([1,2,3], [1,4,6]) - - -This method's opposite is +not_in_all+. - -h4. +matches+ - -Match a specific field with a given value. Use +%+ for wildcard searching. - - -Post.where(Post.arel_table[:author].matches("Ryan%")) - - -This method's opposite is +does_not_match+. - -h4. +matches_any+ - -Match a specific field with any given value. Use +%+ for wildcard searching. - - -Post.where(Post.arel_table[:author].matches_any(["Ryan%", "Yehuda%"])) - - -This method's opposite is +does_not_match_any+ - -h4. +matches_all+ - -Match a specific field with all given values. Use +%+ for wild card searching. - - -Post.where(Post.arel_table[:author].matches_all(["Ryan%", "%Bigg"])) - - -This method's opposite is +does_not_match_all+. - -h4. +gteq+ - -Check for a field greater than or equal to a specific value. - - -Post.where(Post.arel_table[:comments_count].gteq(1)) - - -This method's opposite is +lteq+. - -h4. +gteq_any+ - -Check for a field greater than or equal to any of the given values. - - -Post.where(Post.arel_table[:comments_count].gteq_any([1,2])) - - -This method's opposite is +lteq_any+. - -h4. +gteq_all+ - -Check for a field greater than or equal to all of the given values. - - -Post.where(Post.arel_table[:comments_count].gteq_all([1,2])) - - -This method's opposite is +lteq_all+. - -h4. +or+ - -Allows you to chain queries to get results matching either condition: - - -title = Post.arel_table[:title] -Post.where(title.eq("Active").or(title.eq("Record"))) - - -Note that this method is called on the end of the +eq+ method here, rather than the +where+. - h3. Ordering To retrieve records from the database in a specific order, you can use the +order+ method. -- cgit v1.2.3