Question

So I have the following model which contains a serialized integer attribute

Rake file:

class CreateCompanyAssetStats < ActiveRecord::Migration
  def change
    create_table :company_asset_stats do |t|
      t.integer :companyID
      t.string :geo
      t.integer :assetType
      t.date :startTime
      t.date :endTime

      t.timestamps
    end
  end
end

Model:

serialize :companyID, Array
serialize :assetType, Array
serialize :geo, Array

In my view I have a form where I have a multiselect dropdown that stores the value(s) into params:

  <div class="dropdown" data-init="dropdown">
    <button >Multi Select</button>
    <select name="company_asset_stats[companyID][]" multiple>
      <% allFieldValues('company', 'companyname', 'company').each do |value| %>
        <option value=<%= value[1] %>><%= value[0] %></option>
      <% end %>
    </select>
  </div>

The resulting param is in the following form: Parameters: {"utf8"=>"✓", "authenticity_token"=>"2WFx3/3TyHsjobwRaLJYW2jt4JR5ucvtgyBK3IxKTqs=", "company_asset_stats"=>{"startTime"=>"2013-07-31T00:00-07:00", "endTime"=>"2013-07-31T00:00-07:00", "companyID"=>["1864"]}, "commit"=>"Create"}

And finally my create controller does the following:

def create
    @company_asset_stats = CompanyAssetStats.new(params[:company_asset_stats])

    if @company_asset_stats.save
      redirect_to :action => "index"
    else
      render 'new'
    end
end

When i am redirected to my model's index however, I see that companyID is always saved as 0 and assetType is nothing at all. I have also tried manually setting the attributes in the following manner:

@company_asset_stats.companyID = [1,2,3]

but I get the same display in the index.

Any help?

Was it helpful?

Solution

You appear to have a t.integer :companyID in your database. Serialized attributes cannot go into an integer column, but instead need a text column.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top