Rails serialized integer attribute in params, but not saving into model instance

偶尔善良 提交于 2019-12-08 08:29:47

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17978723/rails-serialized-integer-attribute-in-params-but-not-saving-into-model-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!