Suppose I have the following method.
def create_multiple
project = Project.find(params[:project])
params[:tasks].each_value { |task| @tasks << @project.tasks.build(task) }
project.save
respond_with @tasks
end
I thought about changing it to call .create instead of build to create the tasks because I want them to save if there are no validation errors, even of other tasks fail because of validation. I’m curious what others think about this idea? I thought about using create_or_update as well, but I’m thinking I want to encourage people to remove items that were successfully created rather than just repeatedly calling with the same list until all items are created.
Is there a better way to do this so I can continue to call .save once and have it create ones that pass validation and fails the ones that don’t without failing out entirely?
Not sure exactly what you’re trying to achieve, but if you want to avoid calling save on those which fail validation how about just making new tasks, filtering for valid, and attaching them to the project after that?