Saving enum from select in Rails 4.1

后端 未结 8 1989
难免孤独
难免孤独 2020-11-28 02:31

I am using the enums in Rails 4.1 to keep track of colors of wine.

Wine.rb

class Wine < ActiveRecord::Base
    enum color: [:red,         


        
8条回答
  •  醉话见心
    2020-11-28 03:14

    Here is what worked for me, Rails 4+:

    class Contract < ApplicationRecord
    
    enum status: { active:  "active", 
                   ended: "active", 
                   on_hold: "on_hold", 
                   terminated:  "terminated", 
                   under_review:  "under_review" , 
                   unknown: "unknown" 
                  }
    
    
    end
    

    in my _form.html.erb , I have this:

      
    <%= form.select :status, Contract.statuses.keys, {}%>

    test from Console after adding a record:

    2.3.0 :001 > Contract.last.status
      Contract Load (0.2ms)  SELECT  "contracts".* FROM "contracts" ORDER BY "contracts"."id" DESC LIMIT ?  [["LIMIT", 1]]
     => "active"
    

提交回复
热议问题