aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/active_record_querying.md
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2015-12-10 14:31:25 +0100
committerYves Senn <yves.senn@gmail.com>2015-12-10 14:40:06 +0100
commit0dfca1db2a9d78f7211ce450b5300b0f6f41953e (patch)
treeb7e98501c396cbdefc3bb5d9aeb327644bdbf155 /guides/source/active_record_querying.md
parent3e5a1c2865f78236d4bc2e807b496267b99d8575 (diff)
parent785043b81dd1a866fde6f3da34fe7dec73496433 (diff)
downloadrails-0dfca1db2a9d78f7211ce450b5300b0f6f41953e.tar.gz
rails-0dfca1db2a9d78f7211ce450b5300b0f6f41953e.tar.bz2
rails-0dfca1db2a9d78f7211ce450b5300b0f6f41953e.zip
Merge pull request #22367 from andreynering/docs-ar-enums
Adding Enums section to AR Querying. [ci skip]
Diffstat (limited to 'guides/source/active_record_querying.md')
-rw-r--r--guides/source/active_record_querying.md30
1 files changed, 30 insertions, 0 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md
index c5eba38a78..ed1c3e7061 100644
--- a/guides/source/active_record_querying.md
+++ b/guides/source/active_record_querying.md
@@ -1399,6 +1399,36 @@ 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
+-----
+
+The `enum` macro maps an integer column to a set of possible values.
+
+```ruby
+class Book < ActiveRecord::Base
+ enum availability: [:available, :unavailable]
+end
+```
+
+This will automatically create the corresponding [scopes](#scopes) to query the
+model. Methods to transition between states and query the current state are also
+added.
+
+```ruby
+# Both examples below query just available books.
+Book.available
+# or
+Book.where(availability: :available)
+
+book = Book.new(availability: :available)
+book.available? # => true
+book.unavailable! # => true
+book.available? # => false
+```
+
+Read the full documentation about enums
+[in the Rails API docs](http://api.rubyonrails.org/classes/ActiveRecord/Enum.html).
+
Understanding The Method Chaining
---------------------------------