In this link Rails find_or_create by more than one attribute? can use more that one attribute with active record.
How can I use more than attribute in mongoid?
Thank you
If you look at the source in lib/mongoid/finders.rb:
# Find the first +Document+ given the conditions, or creates a
# with the conditions that were supplied.
...
# @param [ Hash ] attrs The attributes to check.
#
# @return [ Document ] A matching or newly created document.
def find_or_create_by(attrs = {}, &block)
find_or(:create, attrs, &block)
end
you can see that find_or_create_by accepts a {}
as the first argument. You can just pass in several conditions at once
something.find_or_create_by(name: 'john', age: 20)
and it should work.
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