Ruby on Rails 4.1 Release Notes #6(Active Record)

Ruby on Rails 4.1 Release Notes — Ruby on Rails Guides
6章は、Active Recordです。たくさん修正が入っています。

6 Active Record

6.1 Removals

depricationのremove祭りです。
だいたいがそんな機能あったんかって感じです。
・SchemaCache methods: primary_keys, tables, columns and columns_hash が nilを許可しなくなったそうです。
 
・Remove deprecated block filter from ActiveRecord::Migrator#migrate.
・Remove deprecated String constructor from ActiveRecord::Migrator.
・Removed deprecated scope use without passing a callable object.
 
・transaction_joinable の代わりに begin_transaction with d:joinable option 使えということらしいですが
 そもそも transaction_joinable がよくわからん。
 
・Removed deprecated decrement_open_transactions.
・Removed deprecated increment_open_transactions.
・PostgreSQLAdapter#outside_transaction? の代わりに #transaction_open?
ActiveRecord::Fixtures.find_table_name の代わりに ActiveRecord::Fixtures.default_fixture_model_name
・SchemaStatements#columns_for_remove がなくなりました
・SchemaStatements#distinct が無くなりました
ActiveRecord::TestCase がpublicでは無くなりました
・関連の中で使用できた :restrict オプションが無くなりました
・関連の中で使用できた :delete_sql, :insert_sql, :finder_sql and :counter_sql オプションがなくなりました
・type_cast_code がなくなりました
 
ActiveRecord::Base#connection が無くなりました
 これは時々使っていたので注意が必要です。
 
・auto_explain_threshold_in_seconds N秒以上掛かるクエリを自動でexplainする機能が無くなっています。
 PRにはあんまり使われていないから、と書かれています。
 
・Remove deprecated :distinct option from Relation#count.
・Removed deprecated methods partial_updates, partial_updates? and partial_updates=.
・Removed deprecated method scoped
・Removed deprecated method default_scopes?.
・Remove implicit join references that were deprecated in 4.0.
 暗黙的なjoinはダメだそうです。

# before with implicit joins
Comment.where('posts.author_id' => 7)

# after
Comment.references(:posts).where('posts.author_id' => 7)

・Removed activerecord-deprecated_finders as a dependency.
・implicit_readonly の代わりに readonly を使ってね

 

6.2 Deprecations

新たなdepricatonです。

・quoted_locking_column
 楽観的ロック使用時のカラム名を取得するメソッドだったようですが、
 どこからも呼ばれていないしpublicである必要もないのでdeprecatedになったようです。
 
・Deprecated the delegation of Array bang methods for associations.
 2.9 Mutator methods called on Relation to_a してねってやつですね。
 
・Deprecated ConnectionAdapters::SchemaStatements#distinct, as it is no longer used by internals.
・rake db:test:* tasks
 自動でdbがメンテされるのでdeprecatedになったようです。
 
・Deprecate unused ActiveRecord::Base.symbolized_base_class and ActiveRecord::Base.symbolized_sti_name without replacement.

6.3 Notable changes

ActiveRecord::Base.to_param が追加されました。
これは良さそうです。

class User < ActiveRecord::Base
  to_param :name
end

user.id       # => 123
user.to_param # => "123-fancy-pants"

というように書けます。
以前は、以下のようにoverrideする必要がありました。

  def to_param
    "#{id}-#{name}"
  end

 
・touchを無効化する ActiveRecord::Base.no_touching が追加されました。
というか、touch 知らんかった。updated_at を更新してくれるんですね。
で、これを無効化すると。

ActiveRecord::Base.no_touching do
  Project.first.touch  # does nothing
  Message.first.touch  # does nothing
end

Project.no_touching do
  Project.first.touch  # does nothing
  Message.first.touch  # works, but does not touch the associated project
end

 
・MysqlAdapter and Mysql2Adapter で type_cast の戻り値を統一。 return 1 for true and 0 for false.
 
・unscope は default_scope の指定された条件を削除できるようになったようです。
 
・rewhere が追加。条件を上書きできるようです。
 
・cache_key が拡張され、timestampの要素を指定でき、update_atと比べて大きい方が採用されるようです。
 
enum これのことですね
 
・Type cast json values on write, so that the value is consistent with reading from the database.
 JSONでsymbolで渡したものがStringになって一貫性が保てましたってこと(いまいち良く分からん)?
・Type cast hstore values on write, so that the value is consistent with reading from the database.
これも同じような感じで、postgresのhstore 利用時に数字突っ込んだら、文字列になりますよってことかな
・Make next_migration_number accessible for third party generators.
 ライブラリ作者は、これを利用するのにコピペするのが常套手段のようだったのですが
 コピペせんでもよくなったっみたいです。
 
・update_attributes は引数がnilの場合、ArgumentError 例外をあげていましたが、
 stringify_keys を持っていない場合に変更したようです。
・CollectionAssociation#first/#last (e.g. has_many) use a LIMITed query to fetch results rather than loading the entire collection.
 今までは全件持っていたのか。
 
・inspect メソッドは、コネクションを新たに生成しないようなったようです。
 
・Removed column restrictions for count, let the database raise if the SQL is invalid.

User.select("name, username").count
# Before => SELECT count(*) FROM users
# After => ActiveRecord::StatementInvalid

ということだそうで、count:(:all)を使えばこれまで通りの結果が得られます。

User.select("name, username").count(:all) # => SELECT count(*) FROM users

 
・自動でinverse (:inverse_of option)が有効になったようです。
 
・attributes の alias が symbol でも利用できるようなったようです。
 
・ERBのコンテキストのはなしのようです。
2.7 Methods defined in Active Record fixtures  
RAILS_ENVが指定されていれば、test databaseを作成/削除しないようになったようです。

See Also

Ruby on Rails 4.1 Release Notes
#1(Rails4.1へのupgrade方法)
#2(Rails4.1の主な機能)
#3(Railties)
#4(Action Pack)
#5(Action Mailer)
#7(Active Model)
#8(Active Support)