Use update_columns in hstore attribute

Is there an equivalent update_columnsfor attributes hstoreon rails 4?

My model:

class Image < ActiveRecord::Base
  store_accessor :additions, :small, :medium, :big, :image_version
end

Assuming I want to update small.
I tried:

@image = Image.first
@image.update_columns(small: 'my_small_image')

But I get, of course:

PG::UndefinedColumn: ERROR:  column "small" of relation "contents" does not exist
LINE 1: UPDATE "images" SET "small" = 'my_small_image' WHERE "imag...
                              ^
: UPDATE "images" SET "small" = 'my_small_image' WHERE "images"."id" = 1

Is there an easy way to do this?

EDIT: I can't use update_attributesit because I only need to save the passed arguments.

update_attributescalls save, and I don’t want this because it saves all the other changed attributes, not just the ones that were passed.

Example:

@image = Image.first
@image.big = 'fjdiosjfoa'
@image.update_attributes(small: 'my_small_image')

Both large and small are preserved.

Instead, when update_columns, only small ones are preserved. How to do it with hstore?

+4
2

update_attributes(small: 'my_small_image'), .

assign_attributes(small: 'my_small_image'), .

+2

update_column, hstore . hstore, :

@image.update_column(:additions, @image.additions.merge({ 'small' => 'my_small_image' }) )
0

Source: https://habr.com/ru/post/1584909/


All Articles