在Rails ActiveRecord中Upsert

时间:2011-01-14 20:25:05

标签: ruby-on-rails activerecord upsert

ActiveRecord是否具有内置的upsert功能?我知道我可以自己写,但我显然不希望这样的事情已经存在。

7 个答案:

答案 0 :(得分:14)

Model.find_or_initialize可能会做你想要的。如果有意义,您可以使用saveupdate_attributes进行链接。

Rails Guides中的更多信息。

答案 1 :(得分:13)

我刚跑过这个图书馆: https://github.com/seamusabshere/upsert

我还没有测试过,但看起来很有希望

答案 2 :(得分:1)

还有Model.find_or_create

答案 3 :(得分:0)

IMO Upsert机制需要为每个模型进行自定义配置。

因此,最好的解决方案是为模型实现自定义SQL查询,例如

Element subtree:
 →Application 0x7fc1b3f3aa00: {{0.0, 0.0}, {375.0, 667.0}}, label: 'myappname-masked'
    Window 0x7fc1b3f439b0: Main Window, {{0.0, 0.0}, {375.0, 667.0}}
      Other 0x7fc1b3f55c20: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
        Other 0x7fc1b3f43440: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
          Other 0x7fc1b3f31830: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
            Other 0x7fc1b3f61ff0: traits: 8589934592, {{0.0, 0.0}, {375.0, 667.0}}
              Other 0x7fc1b3f52820: traits: 8589934592, {{0.0, 0.0}, {375.0, 64.0}}
                Other 0x7fc1b3f4e190: traits: 8589934592, {{0.0, 0.0}, {375.0, 64.0}}
                  Button 0x7fc1b3f3a250: traits: 8589934593, {{5.0, 20.0}, {44.0, 44.0}}, label: 'search icon right'
                  Other 0x7fc1b3f349b0: traits: 8589935616, {{15.0, 20.0}, {345.0, 39.0}}, label: 'Search field'
                  Image 0x7fc1b3f3b3b0: traits: 8589934596, {{139.0, 22.0}, {97.0, 30.0}}, identifier: 'logo'
                  Button 0x7fc1b3f537a0: traits: 8589934593, {{326.0, 20.0}, {44.0, 44.0}}, label: 'your account icon'
              CollectionView 0x7fc1b3f32450: traits: 35192962023424, {{0.0, 64.0}, {375.0, 603.0}}
                Cell 0x7fc1b3f1e230: traits: 8589934592, {{0.0, 64.0}, {375.0, 50.0}}

......

答案 4 :(得分:0)

rails 6中有一个很棒的新功能:他们在FILE 1 (exclude) . (Start loop from here) . . . (stop) 上添加了upsertupsert_all

更多信息,请点击https://edgeapi.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-upsert_all

答案 5 :(得分:0)

在这种情况下https://github.com/rails/rails/pull/31989

,Rails 6引入了create_or_find_by

对于大量记录,也可以使用https://github.com/zdennis/activerecord-import

示例:

Book.import [book], on_duplicate_key_update: [:title]

答案 6 :(得分:-2)

我写了一篇关于如何实现这一目标的博客文章。请查看here

您必须编写有效的记录扩展名。它看起来像这样。

module ActiveRecordExtension
  extend ActiveSupport::Concern

  def self.upsert(attributes)
    begin
        create(attributes)
    rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation => e
        find_by_primary_key(attributes['primary_key']).
        update(attributes)
    end
  end
end

ActiveRecord::Base.send(:include, ActiveRecordExtension)