ActiveRecord::Persistence#create allows you to pass in an array, and it will create objects from that array (using
new) and return them.
However, ActiveRecord::Inheritance#new does not allow you to do this. Being similar in scope to
create, my expectation is that I should be able to pass in an array of attributes, and get a list of unpersisted objects as a result.
new in Ruby, returns a single instance of an object. Changing new to return an array of objects is counterintuitive.
build should already do what you expect though.
build only works with associations, so it doesn’t quite meet that need.
I don’t disagree that it’s counterintuitive, but I would argue that the same thing can be said about the
create is essentially just a wrapper for
Object.new.save, so it’s odd to me that it has functionality that
new doesn’t share. I expected the two to roughly have feature parity, since they are so closely related.
If it works in association, well, let’s add a build it the class level, not change the meaning of new
It doesn’t quite solve the mismatch in functionality between
create, which is pretty confusing IMO. But, it is a great solution to the immediate “how do I generate multiple unpersisted objects from a hash” problem. I’ll work on a PR for this.