Mongoid is missing the find_or_create_by! method for associations. This exists at a class level so I was expecting it to exist for a many association too.
object.foobars.find_or_create_by!(name: "Test")
I've got around this my re-opening Mongoid::Relations::Many for now, but I'm not sure it covers all eventualities. I've only been using Mongoid for a few days so don't want to submit a pull request!
module Mongoid
module Relations
class Many < Proxy
- Bang method isn't included in Mongoid for relations by default
def find_or_create_by!(attrs = {}, type = nil, &block)
find_or(:create!, attrs, type, &block)
end
end
end
end