加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在hstore属性上使用update_columns

发布时间:2020-12-17 02:24:00 所属栏目:百科 来源:网络整理
导读:rails 4中的hstore属性是否相当于update_columns? 我的模型是: class Image ActiveRecord::Base store_accessor :additions,:small,:medium,:big,:image_versionend 假设我想更新小. 我试过了: @image = Image.first@image.update_columns(small: 'my_sma
rails 4中的hstore属性是否相当于update_columns?

我的模型是:

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

假设我想更新小.
我试过了:

@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_attributes(small:’my_small_image’).

如果您不想保存,请使用assign_attributes(small:’my_small_image’).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读