diff options
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_querying.md | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index 834c4670fb..5c5871f7b1 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -35,7 +35,7 @@ end ```ruby class Order < ActiveRecord::Base - belongs_to :client, :counter_cache => true + belongs_to :client, counter_cache: true end ``` @@ -356,7 +356,7 @@ Two additional options, `:batch_size` and `:start`, are available as well. The `:batch_size` option allows you to specify the number of records to be retrieved in each batch, before being passed individually to the block. For example, to retrieve records in batches of 5000: ```ruby -User.find_each(:batch_size => 5000) do |user| +User.find_each(batch_size: 5000) do |user| NewsLetter.weekly_deliver(user) end ``` @@ -368,7 +368,7 @@ By default, records are fetched in ascending order of the primary key, which mus For example, to send newsletters only to users with the primary key starting from 2000, and to retrieve them in batches of 5000: ```ruby -User.find_each(:start => 2000, :batch_size => 5000) do |user| +User.find_each(start: 2000, batch_size: 5000) do |user| NewsLetter.weekly_deliver(user) end ``` @@ -381,7 +381,7 @@ The `find_in_batches` method is similar to `find_each`, since both retrieve batc ```ruby # Give add_invoices an array of 1000 invoices at a time -Invoice.find_in_batches(:include => :invoice_lines) do |invoices| +Invoice.find_in_batches(include: :invoice_lines) do |invoices| export.add_invoices(invoices) end ``` @@ -443,7 +443,7 @@ Similar to the `(?)` replacement style of params, you can also specify keys/valu ```ruby Client.where("created_at >= :start_date AND created_at <= :end_date", - {:start_date => params[:start_date], :end_date => params[:end_date]}) + {start_date: params[:start_date], end_date: params[:end_date]}) ``` This makes for clearer readability if you have a large number of variable conditions. @@ -457,7 +457,7 @@ NOTE: Only equality, range and subset checking are possible with Hash conditions #### Equality Conditions ```ruby -Client.where(:locked => true) +Client.where(locked: true) ``` The field name can also be a string: @@ -469,16 +469,16 @@ Client.where('locked' => true) In the case of a belongs_to relationship, an association key can be used to specify the model if an ActiveRecord object is used as the value. This method works with polymorphic relationships as well. ```ruby -Post.where(:author => author) -Author.joins(:posts).where(:posts => {:author => author}) +Post.where(author: author) +Author.joins(:posts).where(posts: {author: author}) ``` -NOTE: The values cannot be symbols. For example, you cannot do `Client.where(:status => :active)`. +NOTE: The values cannot be symbols. For example, you cannot do `Client.where(status: :active)`. #### Range Conditions ```ruby -Client.where(:created_at => (Time.now.midnight - 1.day)..Time.now.midnight) +Client.where(created_at: (Time.now.midnight - 1.day)..Time.now.midnight) ``` This will find all clients created yesterday by using a `BETWEEN` SQL statement: @@ -494,7 +494,7 @@ This demonstrates a shorter syntax for the examples in [Array Conditions](#array If you want to find records using the `IN` expression you can pass an array to the conditions hash: ```ruby -Client.where(:orders_count => [1,3,5]) +Client.where(orders_count: [1,3,5]) ``` This code will generate SQL like this: @@ -698,7 +698,7 @@ The `reorder` method overrides the default scope order. For example: class Post < ActiveRecord::Base .. .. - has_many :comments, :order => 'posted_at DESC' + has_many :comments, order: 'posted_at DESC' end Post.find(10).comments.reorder('name') @@ -755,12 +755,12 @@ Post.none # returns an empty Relation and fires no queries. ```ruby # The visible_posts method below is expected to return a Relation. -@posts = current_user.visible_posts.where(:name => params[:name]) +@posts = current_user.visible_posts.where(name: params[:name]) def visible_posts case role when 'Country Manager' - Post.where(:country => country) + Post.where(country: country) when 'Reviewer' Post.published when 'Bad User' @@ -954,7 +954,7 @@ Or, in English: "return all posts that have a category and at least one comment" #### Joining Nested Associations (Single Level) ```ruby -Post.joins(:comments => :guest) +Post.joins(comments: :guest) ``` This produces: @@ -970,7 +970,7 @@ Or, in English: "return all posts that have a comment made by a guest." #### Joining Nested Associations (Multiple Level) ```ruby -Category.joins(:posts => [{:comments => :guest}, :tags]) +Category.joins(posts: [{comments: :guest}, :tags]) ``` This produces: @@ -996,7 +996,7 @@ An alternative and cleaner syntax is to nest the hash conditions: ```ruby time_range = (Time.now.midnight - 1.day)..Time.now.midnight -Client.joins(:orders).where(:orders => {:created_at => time_range}) +Client.joins(:orders).where(orders: {created_at: time_range}) ``` This will find all clients who have orders that were created yesterday, again using a `BETWEEN` SQL expression. @@ -1057,7 +1057,7 @@ This loads all the posts and the associated category and comments for each post. #### Nested Associations Hash ```ruby -Category.includes(:posts => [{:comments => :guest}, :tags]).find(1) +Category.includes(posts: [{comments: :guest}, :tags]).find(1) ``` This will find the category with id 1 and eager load all of the associated posts, the associated posts' tags and comments, and every comment's guest association. @@ -1109,7 +1109,7 @@ Scopes are also chainable within scopes: ```ruby class Post < ActiveRecord::Base - scope :published, -> { where(:published => true) } + scope :published, -> { where(published: true) } scope :published_and_commented, -> { published.where("comments_count > 0") } end ``` @@ -1278,7 +1278,7 @@ second time we run this code, the block will be ignored. You can also use `find_or_create_by!` to raise an exception if the new record is invalid. Validations are not covered on this guide, but let's assume for a moment that you temporarily add ```ruby -validates :orders_count, :presence => true +validates :orders_count, presence: true ``` to your `Client` model. If you try to create a new `Client` without passing an `orders_count`, the record will be invalid and an exception will be raised: @@ -1346,7 +1346,7 @@ Client.connection.select_all("SELECT * FROM clients WHERE id = '1'") `pluck` can be used to query a single or multiple columns from the underlying table of a model. It accepts a list of column names as argument and returns an array of values of the specified columns with the corresponding data type. ```ruby -Client.where(:active => true).pluck(:id) +Client.where(active: true).pluck(:id) # SELECT id FROM clients WHERE active = 1 # => [1, 2, 3] @@ -1415,7 +1415,7 @@ Client.exists?([1,2,3]) It's even possible to use `exists?` without any arguments on a model or a relation. ```ruby -Client.where(:first_name => 'Ryan').exists? +Client.where(first_name: 'Ryan').exists? ``` The above returns `true` if there is at least one client with the `first_name` 'Ryan' and `false` otherwise. @@ -1438,8 +1438,8 @@ Post.recent.any? Post.recent.many? # via a relation -Post.where(:published => true).any? -Post.where(:published => true).many? +Post.where(published: true).any? +Post.where(published: true).many? # via an association Post.first.categories.any? @@ -1461,14 +1461,14 @@ Client.count Or on a relation: ```ruby -Client.where(:first_name => 'Ryan').count +Client.where(first_name: 'Ryan').count # SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan') ``` You can also use various finder methods on a relation for performing complex calculations: ```ruby -Client.includes("orders").where(:first_name => 'Ryan', :orders => {:status => 'received'}).count +Client.includes("orders").where(first_name: 'Ryan', orders: {status: 'received'}).count ``` Which will execute: @@ -1533,7 +1533,7 @@ Running EXPLAIN You can run EXPLAIN on the queries triggered by relations. For example, ```ruby -User.where(:id => 1).joins(:posts).explain +User.where(id: 1).joins(:posts).explain ``` may yield @@ -1572,7 +1572,7 @@ may need the results of previous ones. Because of that, `explain` actually executes the query, and then asks for the query plans. For example, ```ruby -User.where(:id => 1).includes(:posts).explain +User.where(id: 1).includes(:posts).explain ``` yields |