Ruby Rails 3 NoMethodError(未定义的方法'unserialized'u value';for";--[]\n";:字符串):

Ruby Rails 3 NoMethodError(未定义的方法'unserialized'u value';for";--[]\n";:字符串):,ruby,ruby-on-rails-3,postgresql,serialization,yaml,Ruby,Ruby On Rails 3,Postgresql,Serialization,Yaml,我使用的是Rails 3.2.13和postgress。 我只在生产服务器 NoMethodError (undefined method `unserialized_value' for "--- []\n":String): app/controllers/blogs_controller.rb:159:in `content_generators' 我正在序列化数组以将其存储在db中。下面是代码 控制器 class BlogsController

我使用的是Rails 3.2.13和postgress。 我只在
生产服务器

NoMethodError (undefined method `unserialized_value' for "--- []\n":String):
  app/controllers/blogs_controller.rb:159:in `content_generators'
我正在序列化数组以将其存储在db中。下面是代码

控制器
class BlogsController
模型
classblog
迁移
class AddContentgeneratorsToBlog[]添加到\u yaml
结束
结束
我已经使用了序列化。您可以看到
post\u访问
已序列化。这很好用。 但现在,当我添加另一列
content\u generators
时,它开始崩溃。
提前感谢您的帮助。

由于您使用的是postgresql,我强烈建议您使用内置阵列功能:

# Gemfile
gem 'postgres_ext'

class MyMigration
  def change
    add_column :my_table, :that_array_column, :text, array: true, default: []
  end
end

然后删除模型中的
序列化
调用,就这样了。PG序列化数组的行为与模型上YAML序列化数组的行为完全相同,只是db支持它们上的一些查询方法。

我无法理解您的代码。您想如何在
BlogsController#content_generators
操作中获取您的
@users
?我想在content_generators中存储
用户id的数组。是的,我们可以在任何控制器操作中访问任何模型。对于这种情况,您可以忽略我在循环中所做的工作,因为它在开发环境中工作得非常完美。您所说的“在开发环境中工作得非常完美”是什么意思?这三行代码的作用似乎与“在
内容生成器中存储
用户id
的数组”截然不同。
class Blog < ActiveRecord::Base
  serialize :post_access, Array
  serialize :content_generators, Array
  attr_accessible :post_access, :content_generators
end
class AddContentgeneratorsToBlog < ActiveRecord::Migration
  def change
    add_column :blogs, :content_generators, :string, :default => [].to_yaml
  end
end
# Gemfile
gem 'postgres_ext'

class MyMigration
  def change
    add_column :my_table, :that_array_column, :text, array: true, default: []
  end
end