Rails5へのメジャーアップデートに向けて、目玉の新機能(Action Cable, API mode, Rails command)などの目が行きがちですが、Activerecordのちょっとしたメソッドの追加で便利になったりするので、ActiverecordのChnagelogを軽く見てみたいと思います。
以下は私の抜粋です。
ApplicationRecord is a new superclass for all app models, analogous to app controllers subclassing ApplicationController instead of ActionController::Base. This gives apps a single spot to configure app-wide model behavior.
Newly generated applications have app/models/application_record.rb present by default.
Genadi Samokovarov
モデルを定義する際に、ActiveRecord::BaseではApplicationRecordを継承するようになりました。
Introduce after_{create,update,delete}_commit callbacks.
Before:
after_commit :add_to_index_later, on: :create
after_commit :update_in_index_later, on: :update
after_commit :remove_from_index_later, on: :destroy
After:
after_create_commit :add_to_index_later
after_update_commit :update_in_index_later
after_destroy_commit :remove_from_index_later
Fixes #22515.
Genadi Samokovarov
今までなんでないのかと思っていましたが、after_{create,update,delete}_commitのコールバックが追加されました。
Added ActiveRecord::Relation#left_outer_joins.
Example:
User.left_outer_joins(:posts)
# => SELECT "users".* FROM "users" LEFT OUTER JOIN "posts" ON
"posts"."user_id" = "users"."id"
Florian Thomas
LEFT OUTER JOINできるメソッドが追加されました。
Alias ActiveRecord::Relation#left_joins to ActiveRecord::Relation#left_outer_joins.
Takashi Kokubun
left_joinsがleft_outer_joinsのエリアスになります。
Add option to index errors in nested attributes
For models which have nested attributes, errors within those models will now be indexed if :index_errors is specified when defining a has_many relationship, or if its set in the global config.
Example:
class Guitar < ActiveRecord::Base
has_many :tuning_pegs
accepts_nested_attributes_for :tuning_pegs
end
class TuningPeg < ActiveRecord::Base
belongs_to :guitar
validates_numericality_of :pitch
end
# Old style
guitar.errors["tuning_pegs.pitch"] = ["is not a number"]
# New style (if defined globally, or set in has_many_relationship)
guitar.errors["tuning_pegs[1].pitch"] = ["is not a number"]
accepts_nested_attributes_forを使ってバリデーションエラーが出たときに、何番目のインスタンスがエラーになっているかわかりやすくなります。
Add ActiveRecord::Relation#in_batches to work with records and relations in batches.
Available options are of (batch size), load, begin_at, and end_at.
Examples:
Person.in_batches.each_record(&:party_all_night!)
Person.in_batches.update_all(awesome: true)
Person.in_batches.delete_all
Person.in_batches.each do |relation|
relation.delete_all
sleep 10 # Throttles the delete queries
end
Fixes #20933.
Sina Siadat
#in_batchesはブロックにActiveRecord::Relationを渡せます。
belongs_to will now trigger a validation error by default if the association is not present. You can turn this off on a per-association basis with optional: true. (Note this new default only applies to new Rails apps that will be generated with config.active_record.belongs_to_required_by_default = true in initializer.)
Josef Šimánek
belongs_toの値が必須になりました。config.active_record.belongs_to_required_by_defaultでデフォルトの設定を変えるか、belongs_toの設定する際にoptional: trueを追加すれば、必須ではなくなります。
Added the #or method on ActiveRecord::Relation, allowing use of the OR operator to combine WHERE or HAVING clauses.
Example:
Post.where('id = 1').or(Post.where('id = 2'))
# => SELECT * FROM posts WHERE (id = 1) OR (id = 2)
Sean Griffin, Matthew Draper, Gael Muller, Olivier El Mekki
ついに来ましたね。ORで検索ができるようになりました。
Provide :touch option to save() to accommodate saving without updating timestamps.
Fixes #18202.
Dan Olson
saveメソッドにtouchオプションが追加されて、タイムスタンプを更新せずにレコードの更新ができます。