In Rails 4.1, how to find records by enum symbol?
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.
My Enums is not working on where clauses
I just realized that the feature I was trying to use is not available because I have been reading the Edge
version of the documentation, which is probably still not released. Dumb error.
Find objects with a certain Enum attribute value
You can do it like
User.where(role: User.roles['admin'])
Also
User.admin
works for your case
Rails 4.1 Enums: enum.status = nil
I had the same problem. It was caused because the enum field was defined as a string in my schema instead of an integer. In your case, status
is probably defined as a string in your schema.
class CreateReport < ActiveRecord::Migration
def change
create_table :reports do |t|
...
t.integer :status # if this is t.string you get the symptoms described above!
...
end
end
end
Activerecord Group by ENUM
Just map numbers to related string values:
Campaign.all.group(:status).count.map { |k, v| [Campaign.statuses.key(k), v] }.to_h
Saving enum from select in Rails 4.1
Alright, so apparently, you shouldn't send the integer value of the enum to be saved. You should send the text value of the enum.
I changed the input to be the following:
f.input :color, :as => :select, :collection => Wine.colors.keys.to_a
Which generated the following HTML:
<select id="wine_color" name="wine[color]">
<option value=""></option>
<option value="red">red</option>
<option value="white">white</option>
<option value="sparkling">sparkling</option>
</select>
Values went from "0" to "red" and now we're all set.
If you're using a regular ol' rails text_field it's:
f.select :color, Wine.colors.keys.to_a
If you want to have clean human-readable attributes you can also do:
f.select :color, Wine.colors.keys.map { |w| [w.humanize, w] }
Enum in a where query
This should work:
Conversation.active
Rails: How to use i18n with Rails 4 enums
I didn't find any specific pattern either, so I simply added:
en:
user_status:
active: Active
pending: Pending...
archived: Archived
to an arbitrary .yml file. Then in my views:
I18n.t :"user_status.#{user.status}"
How do I test if an ActiveRecord attribute is an Enum?
ActiveRecord::Enum
adds a defined_enums
class attribute to the model - a hash storing the defined enums:
MyModel.defined_enums
#=> {"status"=>{"in_progress"=>0, "accepted"=>1, "approved"=>2, "declined"=>3, "closed"=>4, "cancelled"=>5, "submitted"=>6}}
To test if an attribute is an enum you could use:
MyModel.defined_enums.has_key?('status')
#=> true
Unfortunately, defined_enums
is not documented.
Related Topics
Rails Model Name Conflict with Included Gem
Rails - Invalid Authenticity Token After Deploy
Reraise (Same Exception) After Catching an Exception in Ruby
How to Access Url Helper from Rails Module
How to Describe an Enumeration Column in a Rails 3 Migration
Ruby Mixins: Extend and Include
How to Select Option in Drop Down Using Capybara
Ruby: How to Define a Class Method in a Module
Is There a Way in Ruby/Rails to Execute Code That Is in a String
Puppet/Facter "Could Not Retrieve Fact Fqdn": How to Fix or Circumvent
What Does ':Location => ...' and 'Head :Ok' Mean in the 'Respond_To' Format Statement
Jekyll on Windows: Pygments Not Working
Rails 3 - Restricting Formats for Action in Resource Routes
Rails Rbenv: Rails: Command Not Found
How to Use Ruby Dbi's 'Select_All' VS 'Execute-Fetch/Each-Finish'