-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
In ActiveRecord, the #create_with method is extremely useful in conjunction with #find_or_create_by. Example:
Foo.create_with(phone_number: api_obj.phone_number).find_or_create_by(name: api_obj.name)
Currently, as I understand it, the only way to implement this in Mongoid is to pass a block (which you can also do in AR, of course).
Foo.find_or_create_by(name: api_obj.name) { |foo| foo.phone_number = api_obj.phone_number }
It would be great if #create_with were supported. Two reasons:
1. It's more readable. Nothing in the block syntax tells you that the block is only executed if the object is created and not if it is found already existing; in fact, there's every reason for those unfamiliar with the details of #find_or_create_by to assume that the block is always executed. The #create_with syntax makes it clearer what is executed when.
2. This would further ease transition from ActiveRecord.
- related to
-
MONGOID-4193 Override shared attributes in criteria's selector with a write method's attributes
- Closed