なるようになるブログ

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

rails commit log流し読み(2014/12/11)

2014/12/11分のコミットです。

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

activerecord/CHANGELOG.md


Merge pull request #17970 from ulissesalmeida/foreign-type-has-many-has-one

ActiveRecordの修正です。

has_onehas_many associationにforeign_typeオプションが追加されました。 foreign_typeオプションには、object's typeを格納するカラム名を指定することが出来ます。

以下サンプル。

# 20141211125559_create_images.rb
class CreateImages < ActiveRecord::Migration
  def change
    create_table :images do |t|
      t.integer :imageable_identifier
      t.string :imageable_class

      t.timestamps null: false
    end
  end
end

# image.rb
class Image < ActiveRecord::Base
  belongs_to :imageable, foreign_key: :imageable_identifier, foreign_type: :imageable_class
end

# post.rb
class Post < ActiveRecord::Base
  has_many :images, as: :imageable, foreign_key: :imageable_identifier, foreign_type: :imageable_class
  has_one :main_image, as: :imageable, foreign_key: :imageable_identifier, foreign_type: :imageable_class, class_name: 'Image'
end
post = Post.create!
image = Image.create!
post.main_image = image
# => UPDATE "images" SET "imageable_identifier" = ?, "imageable_class" = ?, "updated_at" = ? WHERE "images"."id" = ?  [["imageable_identifier", 3], ["imageable_class", "Post"], ["updated_at", "2014-12-11 13:06:22.616639"], ["id", 3]]

Fix ProtocolViolation/bind message supplies for polymorphic + pluck or group

activerecord/lib/active_record/relation/calculations.rbの修正です。

Rails 4.2.rc2でpolymorphic + pluckを使用した際に、エラーになるバグがあったのを修正しています。

以下issueより。

class Foo < ActiveRecord::Base
  has_many :bars, as: :object
end
class Bar < ActiveRecord::Base
  belongs_to :object, polymorphic: true
end
Foo.joins(:bars).pluck(:id)
# => ActiveRecord::StatementInvalid: PG::ProtocolViolation: ERROR:  bind message supplies 0 parameters, but prepared statement "" requires 1 : SELECT "foos"."id" FROM "foos" INNER JOIN "bars" ON "bars"."object_id" = "foos"."id" AND "bars"."object_type" = $1

bind_valuesの設定が足りてなかったようです。


Refactor quoted_date

ActiveRecordのConnectionAdapterの修正です。

各adapterで定義されていたquoted_dateメソッドAbstractAdapterに移動しています。


Revert "Merge pull request #17943 from jeremywadsack/doc_cache_importability"

こちらのコミットをrevertしています。

アップグレード時にcacheのネームスペースを変更する必要がある旨説明を追加していたのですが、本来その必要な無く、上手く動作してなかったのはバグだったようです。