提交 698afe11 编写于 作者: G Godfrey Chan

Mention `where.not` in the example

...so it doesn't look like you *have* to use SQL strings for that case (not
anymore!). Would like to replace the SQL string example with something that
you cannot do with the "normal" query API, but I could not come up with a
short, realistic example. Suggestions welcome!
上级 e076d729
...@@ -36,6 +36,7 @@ module ActiveRecord ...@@ -36,6 +36,7 @@ module ActiveRecord
# needs: # needs:
# #
# Conversation.where(status: [:active, :archived]) # Conversation.where(status: [:active, :archived])
# Conversation.where.not(status: :active)
# #
# You can set the default value from the database declaration, like: # You can set the default value from the database declaration, like:
# #
...@@ -69,9 +70,8 @@ module ActiveRecord ...@@ -69,9 +70,8 @@ module ActiveRecord
# Conversation.statuses[:active] # => 0 # Conversation.statuses[:active] # => 0
# Conversation.statuses["archived"] # => 1 # Conversation.statuses["archived"] # => 1
# #
# Use that class method when you need to know the ordinal value of an enum. For # Use that class method when you need to know the ordinal value of an enum.
# example, you can use that when manually building a SQL string inside a `where` # For example, you can use that when manually building SQL strings:
# condition:
# #
# Conversation.where("status <> ?", Conversation.statuses[:archived]) # Conversation.where("status <> ?", Conversation.statuses[:archived])
# #
......
...@@ -42,6 +42,8 @@ class EnumTest < ActiveRecord::TestCase ...@@ -42,6 +42,8 @@ class EnumTest < ActiveRecord::TestCase
refute_equal @book, Book.where(status: :written).first refute_equal @book, Book.where(status: :written).first
assert_equal @book, Book.where(status: [:proposed]).first assert_equal @book, Book.where(status: [:proposed]).first
refute_equal @book, Book.where(status: [:written]).first refute_equal @book, Book.where(status: [:written]).first
refute_equal @book, Book.where.not(status: :proposed).first
assert_equal @book, Book.where.not(status: :written).first
end end
test "find via where with strings" do test "find via where with strings" do
...@@ -49,6 +51,8 @@ class EnumTest < ActiveRecord::TestCase ...@@ -49,6 +51,8 @@ class EnumTest < ActiveRecord::TestCase
refute_equal @book, Book.where(status: "written").first refute_equal @book, Book.where(status: "written").first
assert_equal @book, Book.where(status: ["proposed"]).first assert_equal @book, Book.where(status: ["proposed"]).first
refute_equal @book, Book.where(status: ["written"]).first refute_equal @book, Book.where(status: ["written"]).first
refute_equal @book, Book.where.not(status: "proposed").first
assert_equal @book, Book.where.not(status: "written").first
end end
test "build from scope" do test "build from scope" do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册