I'm currently using:
XXX.find_or_create_by_uuid(XXXX)
Is there a way to do find or build?
Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.
find_or_create_by is a common method used in Rails to find records based on one or many attributes that are input or create a record if it can't find it.
What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.
build ... produces an object with all nil values EXCEPT for list_id. Item. new creates a new item object, with all nil values.
Try XXX.find_or_initialize_by_uuid(XXXX)
Since Rails 4 this is XXX.find_or_initialize_by(uuid: XXXX)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With