简体   繁体   English

更新has_many / belong_to关系中大量关联对象的行(Rails 4,PostgreSQL 9.4,activeadmin)

[英]Update massive number of rows of associated objects in has_many/belong_to relations (Rails 4, postgresql 9.4, activeadmin)

When creating a model Deal, I use an after_create to create prizes on the DealPrize table. 创建模型交易时,我使用after_create在DealPrize表上创建奖品。

Deal and DealPrize have a belong to/has_many relations: a Deal has many Deal prizes and a Dealprize belongs to a Deal. Deal和DealPrize有一个属于/有很多关系:一个Deal有很多Deal奖品,一个Dealprize属于Deal。

It works like this: on my admin panel (using activeadmin), inside a Deal, I have a column 'prize-number' and I use an after_create so that every time the admin creates a new deal, the app takes this prize_number column, and create this volume of prizes (inserting as many rows as necessary=> often more than 300,000) inside the DealPrize table. 它的工作原理是这样的:在我的管理面板(使用activeadmin)上,在Deal中,我有一个“奖品编号”列,并且我使用了after_create,这样,每次管理员创建新交易时,应用程序都会使用这个award_number列,并在DealPrize表中创建此数量的奖金(插入必要的行=>,经常超过300,000)。

So I create a Deal, and automatically, the app creates a huge number of associated objects (prizes) say 300,000. 因此,我创建了一个Deal,并且该应用程序自动创建了大量关联对象(奖品),例如300,000。

The problem is I might wish 2 days later to change the prize number from 300,000 to 272,000 for example (less prizes) or 324,000 (more prizes). 问题是我希望两天后将奖金数从300,000更改为272,000(少奖)或324,000(更多奖)。

How can I update the created Deal and tell my Rails app to update the number of associated Prizes (removing some or adding some) without deleeting and recreating the whole Deal object ? 如何更新创建的交易,并告诉我的Rails应用更新相关奖励的数量(删除一些或添加一些),而又不破坏和重新创建整个Deal对象?

Also, I need a fast way (like the one I use to create the Deal => raw sql and transaction as the number of rows to create at once is very high). 另外,我需要一种快速的方法(就像我用来创建Deal => raw sql和transaction的方法一样,因为一次创建的行数非常高)。

modals Deals.rb 模态Deals.rb

has_many   :deal_prizes,  dependent: :delete_all

after_create :create_deal_prizes

CONNECTION = ActiveRecord::Base.connection.raw_connection

    def create_deal_prizes
      begin 
        CONNECTION.describe_prepared('yokoatxz')
      rescue PG::InvalidSqlStatementName
        CONNECTION.prepare('yokoatxz', 'INSERT INTO deal_prizes (deal_id,created_at,updated_at,admin_user_id) values ($1, $2, $3, $4)') 
      end

      Deal.transaction do  
        self.prizes_number.times do |i| 
          CONNECTION.exec_prepared('yokoatxz',  [ { value: self.id},
                                                  { value: '2009-01-23 20:21:13' },
                                                  { value: '2009-01-23 20:21:13' },
                                                  { value: self.admin_user_id }
                                                ] )
        end
      end
    end

Thanks for your help, Mathieu 谢谢您的帮助,Mathieu

Well I think that if you just wanted to change the number of deal_prizes then you could use a before_save callback on the Deal to test for a difference between prize_number and prize_number_was, and issue either an insert or delete statement appropriately. 好吧,我认为,如果您只想更改deal_prizes的数量,则可以在Deal上使用before_save回调来测试priest_number和prize_number_was之间的差异,并适当地发出insert或delete语句。

I expect for the delete you'd do something like: 我希望删除后您可以执行以下操作:

deal_prizes.limit(prize_number_was - prize_number).delete_all

... if you didn't care which were deleted. ...如果您不在乎哪个被删除。

You would probably want to use a scope on the deal_prizes: 您可能想在deal_prizes上使用范围:

def self.deletable
  where(:taken => false)
end

... to apply the condition by which rows can be deleted though, and then merge this into the association on deal ... ...以应用可以删除行的条件,然后将其合并到交易关联中...

has_many :deletable_deal_prizes, -> {merge(DealPrize.deletable)}, :class_name => "DealPrize"

... so you can: ... 所以你可以:

deleteable_deal_prizes.limit(prize_number_was - prize_number).delete_all

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM