Help us understand the problem. What is going on with this article?

first_or_initializeとfind_or_initialize_by

More than 3 years have passed since last update.

この2つのメソッドは同じような用途で使えますが、どのような使い分けをすればいいのかをまとめてみました。

find_or_initialize_by

# File activerecord/lib/active_record/relation.rb, line 222
def find_or_initialize_by(attributes, &block)
  find_by(attributes) || new(attributes, &block)
end

find_bynewの引数に同じattributesが渡される。
なので、下記のようなJOINしたテーブルを検索条件に含めるとActiveRecord::AssociationTypeMismatchの例外が発生する。

A.joins(:bs).find_or_initialize_by(bs: {id: 1})

first_or_initialize

# File activerecord/lib/active_record/relation.rb, line 161
def first_or_initialize(attributes = nil, &block)
  first || new(attributes, &block)
end

firstがない場合、first_or_initailizeの第一引数でオブジェクトを初期化するので、下記のように検索した条件とは別にAttributeで初期化することができる。

A.where(name: "foo").first_or_initialize(name: "bar")

引数を省略すると、もちらんAttributeはなにも設定されないまま初期化される。

まとめ

  • joinをして、検索条件にjoinしたテーブルが含んでいる場合はfirst_or_initialize
  • 検索条件と初期化したいオブジェクトのAttributeと違う場合はfirst_or_initialize、同じ場合はfind_or_initialize_by

参考リンク

kouyaf77@github
Rubyist
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした