2
这是我的工作结构:Rails模型attr_accessor属性没有保存?
应用程序/模型/ model.rb
class Model < ActiveRecord::Base
attr_accessor :some_var
end
应用程序/模型/ model_controller.rb
class ModelsController < ApplicationController
def show
@model = Model.find(params[:id])
@other_var
if @model.some_var.nil?
@model.some_var = "some value"
@other_var = "some value"
else
@other_var = @model.some_var
end
end
end
每当我运行此代码(例如show方法),if子句被评估为true(例如@ model.some_var == nil)。我如何解决这个问题?我假设attr_accessor的工作原理有什么问题吗?
啊,好的。事实证明,我真的不知道如何保存这个特定的属性,因为我需要some_var作为一个散列,并且我知道我不能简单地将一个列定义为散列,所以现在我要使用序列化完成工作。 – tanookiben
(虽然它已经很长时间以来,你发布了这个问题)如果你使用postgres你可以使用jsonb类型列来存储哈希.. – Aitizazk