Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails序列化无法插入值_Ruby On Rails_Database_Oop_Sqlite - Fatal编程技术网

Ruby on rails Rails序列化无法插入值

Ruby on rails Rails序列化无法插入值,ruby-on-rails,database,oop,sqlite,Ruby On Rails,Database,Oop,Sqlite,我创建了迁移 add_column :users, :read_post_id, :integer 添加到用户模型中 serialize :read_post_id , Array 编辑: 如果我使用 <%= User.find(current_user.id).read_post_id << 3 %> 您需要将read\u post\u id列的数据类型更改为:text,而不是:integer 为什么??因为活动记录使用YAML而不是整数列序列化文本列中的任何

我创建了迁移

add_column :users, :read_post_id, :integer
添加到用户模型中

serialize :read_post_id  , Array
编辑: 如果我使用

<%=  User.find(current_user.id).read_post_id << 3 %>

您需要将
read\u post\u id
列的数据类型更改为
:text
,而不是
:integer

为什么??因为活动记录使用YAML而不是整数列序列化文本列中的任何对象。序列化数据需要进入文本列,而不是整数列。见应收账款文件:

要更改列类型,请创建如下所示的新迁移文件:

class ChangeReadPostIdColumnType < ActiveRecord::Migration
  def change
    change_column :users, :read_post_id, :text
  end
end

您可能还没有看到我的帖子编辑,但我已经完成了从整数到文本的迁移,它仍然不起作用@esther@John好的,只是看看更新后的帖子。您是否可以使用
=
操作符而不是
trued=并且它的作用与@John Oh相同,您是否尝试过更新控制器或模型中的用户对象而不是视图/erb文件中的用户对象
def current_user login_from_session | | login_from_basic_auth end@esther
class ChangeReadPostIdColumnType < ActiveRecord::Migration
  def change
    change_column :users, :read_post_id, :text
  end
end
class ChangeReadPostIdColumnNameAndType < ActiveRecord::Migration
  def change
    change_column :users, :read_post_id, :text
    rename_column :users, :read_post_id, :read_post_ids
  end
end