Pro*_*oGM 5 ruby postgresql ruby-on-rails hstore ruby-on-rails-4
rails 4 中是否有等效的update_columnsforhstore属性?
我的模型是:
class Image < ActiveRecord::Base
store_accessor :additions, :small, :medium, :big, :image_version
end
Run Code Online (Sandbox Code Playgroud)
假设我想更新small.
我试过:
@image = Image.first
@image.update_columns(small: 'my_small_image')
Run Code Online (Sandbox Code Playgroud)
但我当然收到:
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
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来做到这一点?
编辑:我不能使用update_attributes,因为我只需要保存传递的参数。
update_attributes调用save,我不想要这个,因为它保存了所有其他更改的属性,而不仅仅是通过的属性。
例子:
@image = Image.first
@image.big = 'fjdiosjfoa'
@image.update_attributes(small: 'my_small_image')
Run Code Online (Sandbox Code Playgroud)
大大小小的都得救了。
相反,使用update_columns,只有很少的人会被保存。如何用 hstore 做到这一点?
update_attributes(small: 'my_small_image')如果您想保存,请使用。
assign_attributes(small: 'my_small_image')如果您不想保存,请使用。
| 归档时间: |
|
| 查看次数: |
779 次 |
| 最近记录: |