なるようになるブログ

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

rails commit log流し読み(2015/04/18)

2015/04/18分のコミットです。

CHANGELOGにのったコミットは以下の通りです。

activerecord/CHANGELOG.md


[ci skip] Replace list with array

activesupport/lib/active_support/core_ext/array/wrap.rbguides/source/active_support_core_extensions.mdの修正です。

empty list -> empty arrayに説明を修正しています。


Add missing require for String#strip_heredoc

actionpack/lib/action_dispatch/testing/integration.rbの修正です。

xml_http_requestメソッドString#strip_heredocを使用しているのに、active_support/core_ext/string/stripがrequireされてなかったので、requireを追加しています。


Improve documentation [ci skip]

rails guideのActive Record and PostgreSQLの修正です。

UUIDの項にuuid-osspを有効化する必要がある旨注記の追加、及びreferencesメソッドuuid型を指定した場合のexampleを追加しています。


Merge pull request #19787 from Senjai/patch-2

activerecord/lib/active_record/base.rbのdocの修正です。

ActiveRecord::Baseメソッドをオーバーライドし処理を定義しているexampleで、read_attribute / write_attributeメソッドを使用するのではなく、superメソッドで親クラスのメソッドを呼び出すようexampleを修正しています。


Autosave existing records on HMT associations when the parent is new

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

has_many through associationを使用していて、autosaveを有効にしている時、 親オブジェクトを新規に作成し、既に存在している子オブジェクトをassociationに指定した場合に、子オブジェクトがautosaveされなかったのを、autosaveされるよう修正しています。

issueより。

class Post < ActiveRecord::Base
  has_many :catalog_memberships, inverse_of: :post
  has_many :catalogs, through: :catalog_memberships, inverse_of: :posts, autosave: true
end

class CatalogMembership < ActiveRecord::Base
  belongs_to :post, inverse_of: :catalog_memberships
  belongs_to :catalog, inverse_of: :catalog_memberships
end

class Catalog < ActiveRecord::Base
  has_many :catalog_memberships, inverse_of: :catalog
  has_many :posts, through: :catalog_memberships, inverse_of: :catalogs, autosave: true
end

class BugTest < Minitest::Test
  # This test passes
  def test_autosave_has_many_through_on_existing_parent
    catalog = Catalog.create!(title: 'Original Title')
    catalog.title = 'Amended Title'

    post = Post.create!
    post.catalogs = [catalog]
    post.save!

    assert_equal 1, post.catalogs.count
    assert_equal 'Amended Title', catalog.reload.title
  end

  # This test should also pass, but is instead failing
  def test_autosave_has_many_through_on_new_parent
    catalog = Catalog.create!(title: 'Original Title')
    catalog.title = 'Amended Title'

    post = Post.new
    post.catalogs = [catalog]
    post.save!

    assert_equal 1, post.catalogs.count
    assert_equal 'Amended Title', catalog.reload.title
  end
end

両方新規にオブジェクト生成した場合は問題無く、親だけ新規に作成した場合に問題になっていたようです。