Skip to content

Add chapter to Rails about first_or_create & co #44

New issue

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

Open
thisismydesign opened this issue Jul 17, 2020 · 0 comments
Open

Add chapter to Rails about first_or_create & co #44

thisismydesign opened this issue Jul 17, 2020 · 0 comments

Comments

@thisismydesign
Copy link
Member

Initial notes:

Keep in mind caveats of first_or_create, find_or_create_by, first_or_initialize, find_or_initialize_by and upsert

where and first_or_* can be combined to be the same as 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

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

User.find_or_create_by(nil) != User.find_or_create_by(id: nil)

Valid but broken?

User.where(id: id).find_or_initialize_by
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant