Rails在参数中序列化整数属性,但不保存到模型实例中 [英] Rails serialized integer attribute in params, but not saving into model instance

查看:125
本文介绍了Rails在参数中序列化整数属性,但不保存到模型实例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下模型,其中包含一个序列化的整数属性

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

Rake文件:

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

型号:

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

在我的视图中,我有一个表单,我有一个多选下拉列表,将值存储到params中:

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>

生成的参数格式如下:
参数:{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=>创建}

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

当我被重定向到我的模型的索引时,我看到companyID总是保存为0而assetType根本就没有。我还尝试按以下方式手动设置属性:

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.

任何帮助?

推荐答案

您似乎有一个 t.integer:companyID 在您的数据库中。序列化属性不能进入整数列,而是需要文本列。

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.

这篇关于Rails在参数中序列化整数属性,但不保存到模型实例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆