In Rails 4.1, how to find records by enum symbol?

Abdulaziz

Assume I have this model:

class Conversation < ActiveRecord::Base
  enum status: [ :active, :archived ]
end

How can I find all active conversations without using the numeric value of the enum or without having to iterate over each conversation?

I tried doing Conversation.where(status: :active), but it didn't yield any results.

The only solution comes to mind is to iterate over all conversations and select the active ones, but it doesn't look like a good solution.

Conversation.all.select {|conversation| conversation.active? }  

Is there anything I can do about this?

Kensuke Naito

ActiveRecord::Enum provides scopes based on its values.

Just try:

Conversation.active

or

Conversation.archived

Of course, you can create your own scopes as Kyle Decot mentioned.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

rails how to find with no associated records

From Dev

Rails 4 - how to use enum?

From Dev

Is it possible to find all association records to an object for consolidating records in Rails 4

From Dev

How to handle enum values in rails 4

From Dev

Rails 4 enum - how to test equality?

From Dev

Rails 4 + SQL Lite: Find records with substring of big string?

From Dev

How can I store a symbol in a Rails 4 model?

From Dev

cannot find symbol class Currency (enum)

From Dev

How to find records with 2 not condition in rails where clause?

From Dev

How to find index of array of records return from database in rails?

From Dev

Rails how to find records where the has_many relation exists?

From Dev

Rails: Find records with a certain combination of associated records

From Dev

Find records with missing associated records in Rails

From Dev

Rails 4 + enum: How to upcase the values in a collection_select?

From Dev

How to create select for enum attribute in Rails 4 with simple Form

From Dev

Rails 4 - How to get specific records based on where conditions

From Dev

How to compare time from existing records in rails 4 and mongoid

From Dev

How to query for a list of associated records using Rails 4 + PostgreSQL?

From Dev

How to find a symbol for method

From Dev

How to find controller methods defined in Rails 4?

From Dev

How to find the nearest neighbors of 1 Billion records with Spark?

From Dev

How to remove percentage symbol while extracting records

From Dev

Ruby on Rails: how we should write symbol - symbol: vs :symbol =>

From Dev

"No explicit conversion of Symbol into String" for new records in rails 4.0.1 (only)

From Dev

rails 4 activerecord TypeError nil is not a symbol

From Dev

no implicit conversion of Symbol into String, Paperclip + Rails 4

From Dev

rails 4 activerecord TypeError nil is not a symbol

From Dev

Rails find record with multiple associated records

From Dev

Rails: Find records in nested HABTM relation

Related Related

  1. 1

    rails how to find with no associated records

  2. 2

    Rails 4 - how to use enum?

  3. 3

    Is it possible to find all association records to an object for consolidating records in Rails 4

  4. 4

    How to handle enum values in rails 4

  5. 5

    Rails 4 enum - how to test equality?

  6. 6

    Rails 4 + SQL Lite: Find records with substring of big string?

  7. 7

    How can I store a symbol in a Rails 4 model?

  8. 8

    cannot find symbol class Currency (enum)

  9. 9

    How to find records with 2 not condition in rails where clause?

  10. 10

    How to find index of array of records return from database in rails?

  11. 11

    Rails how to find records where the has_many relation exists?

  12. 12

    Rails: Find records with a certain combination of associated records

  13. 13

    Find records with missing associated records in Rails

  14. 14

    Rails 4 + enum: How to upcase the values in a collection_select?

  15. 15

    How to create select for enum attribute in Rails 4 with simple Form

  16. 16

    Rails 4 - How to get specific records based on where conditions

  17. 17

    How to compare time from existing records in rails 4 and mongoid

  18. 18

    How to query for a list of associated records using Rails 4 + PostgreSQL?

  19. 19

    How to find a symbol for method

  20. 20

    How to find controller methods defined in Rails 4?

  21. 21

    How to find the nearest neighbors of 1 Billion records with Spark?

  22. 22

    How to remove percentage symbol while extracting records

  23. 23

    Ruby on Rails: how we should write symbol - symbol: vs :symbol =>

  24. 24

    "No explicit conversion of Symbol into String" for new records in rails 4.0.1 (only)

  25. 25

    rails 4 activerecord TypeError nil is not a symbol

  26. 26

    no implicit conversion of Symbol into String, Paperclip + Rails 4

  27. 27

    rails 4 activerecord TypeError nil is not a symbol

  28. 28

    Rails find record with multiple associated records

  29. 29

    Rails: Find records in nested HABTM relation

HotTag

Archive