The additional difference between find() and find_by() is that find could only be used to search by primary key (usually the 'id') while the find_by() requires and searches by attribute (either passed as hash like Employee. find_by(name: 'Mike') or using the Employee.
Bulk inserts can be performed using newly added methods: insert_all, insert_all! and upsert_all. All of these new methods allow the insertion of multiple records of the same model into the database.
You generally use #clone if you want to copy an object including its internal state. This is what Rails is using with its #dup method on ActiveRecord. It uses #dup to allow you to duplicate a record without its "internal" state (id and timestamps), and leaves #clone up to Ruby to implement.
Because that's the way the architects intended find(id) to work, as indicated in the RDoc:
Find by id - This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). If no record can be found for all of the listed ids, then RecordNotFound will be raised.
If you don't want the exception to be raised, use find_by_id, which will return nil if it can't find an object with the specified id. Your example would then be User.find_by_id(1)
.
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