this is my tables looks like:
class Promo < ActiveRecord::Base {
:id => :integer,
:product_id => :integer,
:promo_code => :string,
:name => :string,
:description => :text,
:user_id => :string,
:email => :string,
:status => :integer,
:category => :integer,
:expire_date => :date,
:user_limit => :integer,
:created_at => :datetime,
:updated_at => :datetime
}
ane here is my model:
class Promo < ActiveRecord::Base
enum category: [ :discount, :rebate, :custom]
enum status: [ :offered, :claimed, :redeemed ]
end
and my view:
<%= f.select :category, options_for_select(Promo.categories.map { |w| w }, @promo.category) %>
which will generate html like this:
<select name="promo[category]" id="promo_category">
<option value="0">discount</option>
<option value="1">rebate</option>
<option value="2">custom</option>
</select>
But when I try to save it, it throw an error which says:
'0' is not a valid category
How to save enum to database? thanks
UPDATE
I have found the link before.
I change it my view like this:
<%= f.select :category, options_for_select(Promo.categories.keys.to_a.map { |w| [w.humanize, w] }, @promo.category) %>
but back to my root problems, its not working, it says that :
ActiveRecord::RecordNotSaved: Failed to save the record
from /home/krismp/.rvm/gems/ruby-2.1.5/gems/activerecord-4.2.3/lib/active_record/persistence.rb:142:in `save!'
why this is happen?