如何保存序列化字段

时间:2013-10-25 13:31:38

标签: ruby-on-rails ruby serialization

我正在尝试将值推送到序列化文本字段(充当数组)。

在控制器中我有

class DeliveriesController < ApplicationController

def new
    @delivery = Delivery.new
end

def create
    @user = current_user
    @user.deliveries.create(params[:delivery])
    @user.recent_addresses.shift if @user.recent_addresses.size >= 10
        @user.recent_addresses.push(params[:delivery][:from_address])
    @user.save
    redirect_to root_path
end

用户模型

serialize :recent_addresses, Array
attr_accessible :recent_addresses
has_many :deliveries

问题是用户没有使用新的近期地址进行保存。 from_address正在控制器中添加,但是当我尝试保存它时回滚并且最近的地址数组为空。

Parameters: {"delivery"=>{"from_address"=>"xyz"}, "commit"=>"Submit"}
SQL (0.6ms)  INSERT INTO "deliveries" ("created_at", "from_address", "user_id") VALUES ($1, $2) RETURNING "id"  [["created_at", Fri, 25 Oct 2013 13:21:50 UTC +00:00], ["from_address", "xyz"], ["user_id", 1]]
   (0.8ms)  COMMIT
   (0.1ms)  BEGIN
   (0.2ms)  ROLLBACK
Redirected to http://localhost:3000/

0 个答案:

没有答案