ruby on rails - How save multiselect values to database -
how can add values string field in database multiple select.
i have field in database:
t.string "types"
and view:
<%= f.select :order, [["one", 1], ["two", 2], ["three", 3]], {}, { :class => 'form-control', :multiple => true } %>
maybe serialize or json goot idea? possible save , simple way read this?
you can use rails serialize on column:
serialize :order, hash
or json (depending want it):
serialize :order, json
however, columns needs of type 'text' not 'string' serialize work, sure create migration change column type.
rails g migration change_order_type_in_table_name class changeordertypeintablename < activerecord::migration def change_column :my_table, :order, :text end def down change_column :my_table, :order, :string end end
Comments
Post a Comment