aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorAndrey Nering <andrey.nering@gmail.com>2015-11-21 14:29:03 -0200
committerAndrey Nering <andrey.nering@gmail.com>2015-12-08 08:31:56 -0200
commit785043b81dd1a866fde6f3da34fe7dec73496433 (patch)
treeeca0af0c51bb0f70444ae6cb036c159a9c19b70f /guides
parenta61e4ae58d65d43a97e90bdb02b6c407791e3c53 (diff)
downloadrails-785043b81dd1a866fde6f3da34fe7dec73496433.tar.gz
rails-785043b81dd1a866fde6f3da34fe7dec73496433.tar.bz2
rails-785043b81dd1a866fde6f3da34fe7dec73496433.zip
Adding Enums section to Active Record Querying Guide. [ci skip]
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_record_querying.md25
1 files changed, 25 insertions, 0 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md
index c5eba38a78..f0e4ee83c5 100644
--- a/guides/source/active_record_querying.md
+++ b/guides/source/active_record_querying.md
@@ -1399,6 +1399,31 @@ You can specify an exclamation point (`!`) on the end of the dynamic finders to
If you want to find both by name and locked, you can chain these finders together by simply typing "`and`" between the fields. For example, `Client.find_by_first_name_and_locked("Ryan", true)`.
+Enums
+-----
+
+Rails have a convenient keyword `enum`, used to map an integer column to a set
+of possible values.
+
+```ruby
+class Book < ActiveRecord::Base
+ enum availability: [:available, :unavailable]
+end
+```
+
+Enums are a simple way to reduce boilerplate code, since it automatically
+creates [scopes](#scopes) and adds some sintax sugar while querying.
+
+```ruby
+# Both examples below query just available books.
+Book.available
+# or
+Book.where(availability: :available)
+```
+
+Read the full documentation for enums
+[in the Rails API](http://api.rubyonrails.org/classes/ActiveRecord/Enum.html).
+
Understanding The Method Chaining
---------------------------------