attr_accessor not updating value from rails model

526 Views Asked by At

I have the following 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.

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}

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

1

There are 1 best solutions below

0
On

You script is fine, you'll find the below script working perfectly in your rails console:

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.