diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 11 | ||||
-rw-r--r-- | activerecord/test/cases/enum_test.rb | 15 |
2 files changed, 20 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 3086f13f96..5cb80d185b 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -32,6 +32,12 @@ module ActiveRecord # Conversation.active # Conversation.archived # + # Of course, you can also query them directly if the scopes doesn't fit your + # needs: + # + # Conversation.where(status: [:active, :archived]) + # Conversation.where("status <> ?", :active) + # # You can set the default value from the database declaration, like: # # create_table :conversations do |t| @@ -63,11 +69,6 @@ module ActiveRecord # # Conversation.statuses # => { "active" => 0, "archived" => 1 } # - # Use that class method when you need to know the ordinal value of an enum: - # - # Conversation.where("status <> ?", Conversation.statuses[:archived]) - # - # Where conditions on an enum attribute must use the ordinal value of an enum. module Enum def self.extended(base) # :nodoc: base.class_attribute(:defined_enums) diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index ed568413a2..ed1585da1a 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -32,9 +32,22 @@ class EnumTest < ActiveRecord::TestCase assert Book.where(status: Book.statuses[:proposed]).build.proposed? end - test "find via where" do + test "find via where with symbols" do + assert_equal @book, Book.where(format: :paperback).first + refute_equal @book, Book.where(format: :ebook).first + assert_equal @book, Book.where(format: [:paperback]).first + refute_equal @book, Book.where(format: [:ebook]).first + refute_equal @book, Book.where("format <> ?", :paperback).first + assert_equal @book, Book.where("format <> ?", :ebook).first + end + + test "find via where with strings" do assert_equal @book, Book.where(status: "proposed").first refute_equal @book, Book.where(status: "written").first + assert_equal @book, Book.where(status: ["proposed"]).first + refute_equal @book, Book.where(status: ["written"]).first + refute_equal @book, Book.where("format <> ?", "paperback").first + assert_equal @book, Book.where("format <> ?", "ebook").first end test "update by declaration" do |