We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial notes:
first_or_create
find_or_create_by
first_or_initialize
find_or_initialize_by
upsert
where and first_or_* can be combined to be the same as find_or_*_by
where
first_or_*
find_or_*_by
User.where(name: "Roger").first_or_initialize == User.find_or_initialize_by(name: "Roger") User.where(name: "Roger").first_or_create == User.find_or_create_by(name: "Roger")
The body only runs if the object is newly created
User.where(id: id).first_or_create! do |user| user.assign_attributes(attributes) end User.find_or_create_by!(id: id) do |user| user.assign_attributes(attributes) end User.where(id: id).first_or_initialize do |user| user.assign_attributes(attributes) user.save! end User.find_or_initialize_by(id: id) do |user| user.assign_attributes(attributes) user.save! end
Passed attributes are only assigned if the object is newly created
attributes
User.where(id: id).first_or_create!(attributes) User.where(id: id).first_or_initialize(attributes)
find_or_create_by(nil) will get the first entity while find_or_create_by(id: nil) will create a new one
find_or_create_by(nil)
User.find_or_create_by(nil) != User.find_or_create_by(id: nil)
Valid but broken?
User.where(id: id).find_or_initialize_by
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Initial notes:
Keep in mind caveats of
first_or_create
,find_or_create_by
,first_or_initialize
,find_or_initialize_by
andupsert
where
andfirst_or_*
can be combined to be the same asfind_or_*_by
The body only runs if the object is newly created
Passed
attributes
are only assigned if the object is newly createdfind_or_create_by(nil)
will get the first entity while find_or_create_by(id: nil) will create a new oneValid but broken?
The text was updated successfully, but these errors were encountered: