TODO: PostgreSQL 8.3+ includes ENUM type so this is not MySQL specific anymore.
ActiveRecord does not support the SET and ENUM column types directly. You should probably not use non-standard, mySQL-specific column types like SET or ENUM in your Rails applications.
It is recommended that you use VARCHAR columns along with Validations in situations that require ENUM or SET-like behaviour.
Another possibility is this plugin:
Enum Column in MySql
In particular, see validates_inclusion_of to check that the values submitted are allowed in that particular column, or read below for quick reference:
In your model put something like this:
Statuses = {
:active => "ACTIVE",
:inactive =>"INACTIVE",
:unspecified => nil
}
validates_inclusion_of :status, :in => Statuses.values
——
For SET type, let’s use a multiple selection in the view, however, you should not use API like
<%= select(object, method, choices, options = {}, html_options = { :multiple => "" }) %>
<select name="object[method]" multiple="multiple">
<option value="choice_1">choice_1
….
params[:object][:method] receives only one item.
Instead, write HTML code directly like
<select id="object_method" name="object[method][]" multiple="multiple">
<% for choice in choices %>
<% if @object.method and @object.method.split(',').include?(choice) %>
<option value="<%= choice %>" selected><%= choice %>
<% else %>
<% end %>
<% end %>
Ctrl+Click to choose multiple items.
name="object[method][]". You can receive multiple items as an array. (The if statement is to be automatically selected in the edit view.)
In your controller, you will convert the received array to string in the form of “item,item,item”.
Then, the ‘save’ function can handle it.
if params[:object][:method].instance_of?(Array)
params[:object][:method] = params[:object][:method].join(',')
else
params[:object][:method] = ""
end
@object = Object.new(params[:object])
@object.save
<select id="object_method" name="object[method]">
<% for choice in choices %>
<% if @object.method and @object.method == choice %>
<option value="<%= choice %>" selected="selected"><%= choice %>
<% else %>
<% end %>
<% end %>
<%= select 'object', 'method', choices, options = {}, html_options = {} %>
Types = %w(Air Environmental Health Safety Occupancy Water)
validates_inclusion_of :type, :in => Types
<%= select_tag :type, options_for_select(Model::Types) %>
TODO: PostgreSQL 8.3+ includes ENUM type so this is not MySQL specific anymore.
ActiveRecord does not support the SET and ENUM column types directly. You should probably not use non-standard, mySQL-specific column types like SET or ENUM in your Rails applications.
It is recommended that you use VARCHAR columns along with Validations in situations that require ENUM or SET-like behaviour.
Another possibility is this plugin:
Enum Column in MySql
In particular, see validates_inclusion_of to check that the values submitted are allowed in that particular column, or read below for quick reference:
In your model put something like this:
Statuses = {
:active => "ACTIVE",
:inactive =>"INACTIVE",
:unspecified => nil
}
validates_inclusion_of :status, :in => Statuses.values
——
For SET type, let’s use a multiple selection in the view, however, you should not use API like
<%= select(object, method, choices, options = {}, html_options = { :multiple => "" }) %>
<select name="object[method]" multiple="multiple">
<option value="choice_1">choice_1
….
params[:object][:method] receives only one item.
Instead, write HTML code directly like
<select id="object_method" name="object[method][]" multiple="multiple">
<% for choice in choices %>
<% if @object.method and @object.method.split(',').include?(choice) %>
<option value="<%= choice %>" selected><%= choice %>
<% else %>
<% end %>
<% end %>
Ctrl+Click to choose multiple items.
name="object[method][]". You can receive multiple items as an array. (The if statement is to be automatically selected in the edit view.)
In your controller, you will convert the received array to string in the form of “item,item,item”.
Then, the ‘save’ function can handle it.
if params[:object][:method].instance_of?(Array)
params[:object][:method] = params[:object][:method].join(',')
else
params[:object][:method] = ""
end
@object = Object.new(params[:object])
@object.save
<select id="object_method" name="object[method]">
<% for choice in choices %>
<% if @object.method and @object.method == choice %>
<option value="<%= choice %>" selected="selected"><%= choice %>
<% else %>
<% end %>
<% end %>
<%= select 'object', 'method', choices, options = {}, html_options = {} %>
Types = %w(Air Environmental Health Safety Occupancy Water)
validates_inclusion_of :type, :in => Types
<%= select_tag :type, options_for_select(Model::Types) %>