[英]attr_accessor not updating value from rails model
I have the following model我有以下 model
class Job < ActiveRecord::Base
attr_accessor :incentive
end
I want to be able to store a temporary column in my model via attr_accessor.我希望能够通过 attr_accessor 在我的 model 中存储一个临时列。
I want to be able to do something like this我希望能够做这样的事情
job = Job.last
job.incentive = {id: 1}
and i expect if i do job.incentive
, it should return {id: 1}
我希望如果我做job.incentive
,它应该返回{id: 1}
I also tried doing this as well我也尝试过这样做
def incentive =(val)
@incentive = val
end
def incentive
@incentive
end
But that also didn't work.但这也没有用。 How can i be able to store temporary column values in rails 4我如何能够在 rails 4 中存储临时列值
You script is fine, you'll find the below script working perfectly in your rails console:您的脚本很好,您会发现以下脚本在您的 rails 控制台中完美运行:
job = Job.last
job.incentive = { id: 1 }
p job.incentive # returns { id: 1 }
If you restart or refresh your console (or webpage) this information is gone, since it is only set in memory and not stored to the database.如果您重新启动或刷新您的控制台(或网页),此信息将消失,因为它仅在 memory 中设置,并未存储到数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.