なるようになるブログ

読書感想文かrailsについてかrubyについてか

rails commit log流し読み(2016/09/30)

2016/09/30分のコミットです。

CHANGELOGへの追加はありませんでした。


Don't skip in-memory insertion of associations when loaded in validate

activerecord/lib/active_record/associations/collection_association.rbactiverecord/lib/active_record/associations/has_many_through_association.rbの修正です。

validate処理中にassociationsを呼び出した際に、associationsへの追加処理が正しく行われないバグがあったのを修正しています。

issueより。

class GiftCardValidator < ActiveModel::Validator
  def validate(model)
    puts "count inside validator: #{model.checkout.applied_gift_cards.size}"
  end
end

class Checkout < ActiveRecord::Base
  has_many :applied_gift_cards
end

class AppliedGiftCard < ActiveRecord::Base
  belongs_to :checkout

  validates :checkout, presence: true
  validates_with GiftCardValidator
end

checkout = Checkout.create!
checkout.applied_gift_cards.create!
checkout.applied_gift_cards.create!

puts "records created: #{checkout.applied_gift_cards.size}"  # => 1
checkout.reload
puts "records created: #{checkout.applied_gift_cards.size}"  # => 2

AppliedGiftCardは2回作成されているので、最初のcheckout.applied_gift_cards.sizeを出力する際2になるべきなのですが、1になってしまっていた(データは作られていたが、メモリ上のassociationへの追加が行われていなかった)のを、正しく値が表示されるよう修正しています。


Tighten the backtrace pollution from passing through callbacks

activesupport/lib/active_support/callbacks.rbの修正です。

callback内でexceptionが発生した場合に、そのexceptionのbacktraceから不要なcallbackの行を含まないよう修正しています。

PRより。

Before:

RuntimeError: inside save
    test/callbacks_test.rb:73:in `block in save'
    lib/active_support/callbacks.rb:126:in `call'
    lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
    lib/active_support/callbacks.rb:455:in `call'
    lib/active_support/callbacks.rb:101:in `__run_callbacks__'
    lib/active_support/callbacks.rb:755:in `_run_save_callbacks'
    lib/active_support/callbacks.rb:90:in `run_callbacks'
    test/callbacks_test.rb:72:in `save'
    test/callbacks_test.rb:526:in `test_save_person'

After:

RuntimeError: inside save
    test/callbacks_test.rb:73:in `block in save'
    lib/active_support/callbacks.rb:133:in `run_callbacks'
    test/callbacks_test.rb:72:in `save'
    test/callbacks_test.rb:526:in `test_save_person'