The value of Store_accessor is not modified from the database?

Viewed 50

In my model I have results

store_accessor :results

and in controller its like

        if current_user.test
          @results = current_user.test.results
        end

And I am printing this value at the front end it keeps showing 0

    def update
      stats = user.test
      stats.data_will_change!
      attributes = { results: "#{(stats.results.to_i) + 1}" }
      stats.update_attributes(attributes)
    end

It's going through the function but the value at the front-end is still 0

1 Answers

store_accessor requires at least two attributes

store_accessor :column_name, :attribute

:column_name column is presumed to be Hash like type eg Hstore in PostgreSQL or json or something like that.

:attribute than behaves like standard database column in ActiveRecord model.

You can then call something like:

model.attribute = 5
model.save

It also works with mass assignment

model.update(attribute: 5)

In your case it could look something like this:

store_accessor :results, :stats
    def update
      stats = user.test
      stats.update_attributes(stats: stats.results.to_i + 1)
    end
Related