なるようになるブログ

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

rails commit log流し読み(2017/04/10)

2017/04/10分のコミットです。

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

activesupport/CHANGELOG.md


Merge pull request #28713 from kamipo/expose_queries_for_association_queries

Active Recordの修正です。

AssociationQueryValuePolymorphicArrayValuequeriesメソッドを追加し、queryを外から取得出来るよう修正しています。


exclude ORDER BY clause for exists? (#28699)

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

exists?メソッドで、ORDER BY句を除外するよう修正しています。

# before
Model.order(status: :desc).any?
# SELECT  1 AS one FROM `models` ORDER BY `models`.`status` DESC LIMIT 1

# after
Model.order(status: :desc).any?
# SELECT COUNT(*) FROM `models`

Merge pull request #26981 from kamipo/should_not_except_order_for_existsでORDER BY句を維持するよう修正したのですが、 下位互換を維持する為(ORDER BYが設定されている事によりSQLエラーになるのを防ぐ為)、修正されたようです。

なお、exists?の挙動を変えたい場合は、construct_relation_for_existsを上書きすれば出来るようになっています。参考:Extract construct_relation_for_exists in FinderMethods


Implement fetch_values for HashWithIndifferentAccess (#28316)

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

HashWithIndifferentAccessfetch_valuesメソッドを追加しています。挙動はRuby#fetch_valuesと同じです。

hash = ActiveSupport::HashWithIndifferentAccess.new
hash[:a] = 'x'
hash[:b] = 'y'
hash.fetch_values('a', 'b') # => ["x", "y"]
hash.fetch_values('a', 'c') { |key| 'z' } # => ["x", "z"]
hash.fetch_values('a', 'c') # => KeyError: key not found: "c"

[ci skip] documented issue related before_destroy

rails guideのActive Record Callbacksの修正です。

Destroying an Objectの項に、associationのrecord削除時にcallbackを実行したい場合、before_destroy callbackはdependent: :destroy associationsより先に定義する必要がある(:dependentはcallbackを使用しており、callbackは定義された順に実行される為)旨説明を追加しています


Pluralize callback

rails guideのActive Record Callbacksの修正です。

callback -> callbacksに修正しています。