diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-12-10 14:31:25 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-12-10 14:40:06 +0100 |
commit | 0dfca1db2a9d78f7211ce450b5300b0f6f41953e (patch) | |
tree | b7e98501c396cbdefc3bb5d9aeb327644bdbf155 /guides | |
parent | 3e5a1c2865f78236d4bc2e807b496267b99d8575 (diff) | |
parent | 785043b81dd1a866fde6f3da34fe7dec73496433 (diff) | |
download | rails-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')
-rw-r--r-- | guides/source/active_record_querying.md | 30 |
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 --------------------------------- |