Rails 4中的update_columns属性是否具有等效的hstore

我的模型是:

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

假设我要更新small
我试过了:
@image = Image.first
@image.update_columns(small: 'my_small_image')

但是我当然会收到:
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

有一个简单的方法吗?

编辑:我不能使用update_attributes,因为我只需要保存传递的参数
update_attributes调用save,我不希望这样做,因为它保存了所有其他更改的属性,而不仅仅是保存通过的属性。

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

大大小小的都可以保存。

相反,使用update_columns,只会保存很少的内容。如何使用hstore?

最佳答案

使用update_column,但传入hstore属性和哈希值。为了防止任何现有的hstore值被吹走,您需要合并现有的值:

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

10-07 13:21
查看更多