Topic: select many field is only writing one result to table

Hello,
I am running in to a problem with a select field that will allow multiple selections, but will only write the first selection to the table if multiple options are selected.  For example if 'AL', 'AK', and 'AZ are all selected, only 'AL' is being written to the table.

in my view the code is:
<%= select 'applicanth', 'dlstates', ['AL', 'AK', 'AZ', 'AR', ..., 'NA'], { :include_blank => :true }, "multiple" => true %>

in my controller the code is: 
  def create 
    @applicanth = Applicanth.find(session[:id])
    @applicanth.dtelstupd = Date.today
    params[:applicantoffenseds].each_value do |applicantoffensed|   
      @applicanth.applicantoffenseds.build(applicantoffensed) unless applicantoffensed.values.all?(&:blank?) 
    end 
    if @applicanth.update_attributes(params[:applicanth]) 
      redirect_to :controller => 'applicationonline5', :action => 'new'
    else   
      render :action => 'new' 
    end
  end

And the in the model the code is:
class Applicanth < ActiveRecord::Base
  has_many :applicantcontactds
  has_many :applicantreferencesds
  has_many :applicantpositionds
  has_many :applicanteducationds
  has_many :applicantoffenseds
  has_many :applicantreferralds

  has_one :applicantproffesionald
  has_one :applicantpolicyd
  has_one :applicantemploymentd 
  has_one :applicantaad

end

Any suggestions would be appreciated.
Thank you for your help!

Re: select many field is only writing one result to table

Is there only one dlstates column per applicanth model/table record? I'm not certain how you expect it to store multiple states.

Railscasts - Free Ruby on Rails Screencasts

Re: select many field is only writing one result to table

There is only one column for dlstates in the applicanth table.  From your response, I assume that if I created a new table (dlstates) with multiple columns (state1, state2, state3, ..., staten), this would work after I changed the controller and view to reflect the new table.  Is this correct?

Re: select many field is only writing one result to table

It might work, but this isn't the correct approach. Whenever you see numbers in column names (state1, state2, etc.) there is usually a better way to do this. You are right about moving the states into a separate table, but only have one state column there. You can then set up a has_many/belongs_to association between the two models so a given applicant can have many states:

def Applicanth < ActiveRecord::Base
  has_many :applicant_states
end

def ApplicantState < ActiveRecord::Base
  belongs_to :applicanth
end


You will then need to create the state models manually in the controller by looping through the selected states. This can get tricky, but if you need help with that I can post some code.

Railscasts - Free Ruby on Rails Screencasts

Re: select many field is only writing one result to table

An example would be greatly appreciated.  Thank you for the help you have already given!