Common Mistakes When Querying with Enums
❌ Using Strings Instead of Symbols
❌ Incorrect:
User.where(status: "active") # Won’t work properly
✅ Correct:
User.where(status: :active)
❌ Querying with Invalid Enum Keys
If you query with a non-existent enum key, Rails won’t raise an error, but it won’t return expected results.
User.where(status: :unknown) # Returns an empty array instead of raising an error
✅ Solution: Use User.statuses.keys to check available values.
User.statuses.keys # => ["active", "inactive", "banned"]
❌ Forgetting to Handle NULL Values
If a database column allows NULL, filtering for an enum may miss records that have NULL values.
✅ Solution: Always handle NULL explicitly.
User.where(status: nil) # Fetches records with NULL status