Edit: The code has been updated due to the awesome information presented in the comments.
For a project we’ve been working on at Fudge Studios, we had to create four (uurgh) models from one form. After a lot of digging around and some trial and error we came up with a method which works well for us. I thought I’d document it for others as I couldn’t find a lot about it when originally trying do it.
Background
Say we have 3 models that are all mutually exculise from each other but we need to make sure that all of the models are valid before we save them. The original pplan was to use something similiar to the following:
if @person.save && @cat.save && @dog.save
// Woohoo!
else
// Epic fail
end
The problem with this method is: if @person and @dog are valid and save correctly but @dog fails due to validation, then you end up with a person and cat in your database when you don’t want them. This leads to infinite amounts of problems, as rSpec has happily announced on more than one occassion.
The second thing I tried was checking that all the models are valid first then saving them:
if @person.valid? && @cat.valid? && @dog.valid?
@person.save
@cat.save
@dog.save
else
// Epic fail
end
While this looks good on the controller side, it actually sucks. If person is invalid then the errors are shown on the page as expected, but you don’t to see if any of the other models have any errors because valid? doesn’t get called on them. Aaargh!
The Solution
The View
Did you know you could pass in more than one model to error_messages_for? No, neither did I? What about fields_for? It’s a life saver. Let’s look at an example form for a Person, Cat and a Dog:
<% form_for @person do |f| %>
<%= error_messages_for :object => [@person, @cat, @dog] %>
<fieldset>
<legend>Person Details</legend>
<ol>
<li>
<%= f.label :name %>
<%= f.text_field :name %>
</li>
</ol>
</fieldset>
<% fields_for :cat do |cat| %>
<fieldset>
<legend>Cat Details</legend>
<ol>
<li>
<%= cat.label :breed %>
<%= cat.text_field :breed %>
</li>
</ol>
</fieldset>
<% end %>
<% fields_for :dog do |dog| %>
<fieldset>
<legend>Dog Details</legend>
<ol>
<li>
<%= dog.label :breed %>
<%= dog.text_field :breed %>
</li>
</ol>
</fieldset>
<% end %>
<%= f.submit 'Pow!' %>
<% end %>
The main things to look out for here are error_messages_for and fields_for. We pass in an array of the objects we want to display errors for into error_messages_for using :object. This will display all the errors for those models, in our case the person cat and dog errors. Although you need to make sure all the errors for these models are being raised. We’ll look at this later on.
The other thing to take a look at is fields_for. I’ll let the API docs explain this for you:
Creates a scope around a specific model object like form_for, but doesnât create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form.
So that’s our views done. On to the controller:
The Controller
This is the way we’ve been coping with the problem of validating all the models. I’m sure other people will have other suggestions, but this is what we’re rocking:
def new
@person = Person.new
@cat = Cat.new
@dog = Dog.new
end
def create
@person = Person.new(params[:person])
@cat = Cat.new(params[:cat])
@dog = Dog.new(params[:dog])
# Run valid? on each model and check for failures
if [@person, @cat, @dog].all?(&:valid?)
Person.transaction do
@person.save!
@cat.save!
@dog.save!
end
else
// Epic fail
end
end
The only line that you really need to checkout here is the line that runs valid? on each model and check results:
if [@person, @cat, @dog].all?(&:valid?)
This line runs through each model and runs the valid? method and checks that all the results are true.
As pointed out in the comments, this line could be replaced with:
if @person.valid? & @cat.valid? & @dog.valid?
The Person.transaction block makes sure that if one of the models fails to save then the other models aren’t saved as well. This stops you ending up with random saved models that shouldn’t be there.
Bosh.
Further Things to Read with Your Eyes
- James Golick’s ActivePresenter
- Rails Presenter Pattern
- Nested Models in Rails 2.2 (possibly)
- Complex Forms at RailsCasts
- Ryan Bates’ Complex Form examples at Github
(Possibly) Related Posts
- Paperclip: Attaching Files in Rails
- Converting Videos with Rails: Converting the Video
- SWFUpload, Paperclip and Ruby on Rails
If you found this post or anything else on this site of any use, then please take the time to recommend me on Working with Rails.

31 Responses to “Multi Model Forms & Validations in Ruby on Rails”
You might have some of your heartache solved with James Golick’s Active Presenter.
sweet! I like the simplicity of this as opposed to a transaction block!
I’d replace
unless [@person, @cat, @dog].map(&;:valid?).include?(false)
with
if [@person.valid?, @cat.valid?, @dog.valid?].all?
Try google for “Presenter pattern” :-)
http://blog.jayfields.com/2007/03/rails-presenter-pattern.html
In your original solution you could also use a single ampersand:
@person.valid? & @cat.valid? & @dog.valid?
This should force your if statement to evaluate each term
Great Post! I’ve run into this problem a couple times before, and this is (in my opinion) a good solution. I’ll be bookmarking this page for future reference; thanks for putting it up.
Good solution!
I have used something a little different…I saw in the Recipe 18, by Rick Olson (aka Technoweenie), of Advanced Rails Recipe book that he creates a model service class where goes all the stuff for validating and saving (and other fancy things if you need) all models uploaded in the form. This way the controller keeps really thin.
Interesting case. I think this kind of logic can be moved into a dedicated object to make cleaner the controller.
Just a syntax improvement : if [@person, @cat, @dog].all?(&:valid?)
That’s an elegant way of doing the validations, I would also use a transaction to be extra cautious:
unless [@person, @cat, @dog].map(&;:valid?).include?(false)
Person.transaction do
@person.save!
@cat.save!
@dog.save!
end
else…
Very interesting way to get this done. I’ve not ever needed to assign concurrent objects in a single form but the problem I’ve run into several times before is nested model mass assignment. Good news everyone! An upcoming rails release will sport this very feature (probably 2.2): http://ryandaigle.com/articles/2008/7/19/what-s-new-in-edge-rails-nested-models
You really should consider James Golick & Daniel Haran’s ActivePresenter
Hey, nice solution. You should also consider using transactions when saving your 3 models or you could end up with a saved dog and a saved person but not a saved cat: just think what would happen if you have a validates_uniqueness_of :name on your Cat model that prohibits the cat being saved just because on that same moment someone saves an equally named cat while you were busy checking if it was valid or not. :)
You can do something like this as well:
if [@person.valid?, @cat.valid?, @dog.valid?].all?
how about:
if [@person, @cat, @dog].all?(&:valid?)
You can just put first “bad” snippet in the transaction and use save! instead of save.
Oh, and there’s also a missing transaction even if you use code proposed at this website. This code can handle invalid data, but it will make database incosistency when e.g. your MySQL will run out of space before first and second save (when all models are valid!). It’s not paranoid, it’s just a good programming habit.
Ryan Bates highlighted a solution, awhile back, that would clean up your controller a bit (no need for the multiple “valid?” and “save” calls):
http://railscasts.com/episodes?search=complex+forms
More recently, he put together a Github project that has other examples:
http://github.com/ryanb/complex-form-examples/tree/master
What about transactions ? You should wrape the calls to save in a transaction if it is a “all or nothing” scenario.
And no… i did not know that you can pass an array of objects to error_messages_for(). thanks.
Nice. I wonder if what you think of this
http://jamesgolick.com/2008/7/28/introducing-activepresenter-the-presenter-library-you-already-know
You could also use
&instead of&&in yourif’s.So instead of writing
if [@person, @cat, @dog].map(&:valid?).include?(false), you could writeif @person.valid? & @cat.valid? & @dog.valid?.I didn’t know you could do that either!
I’m currently working on a project that will require this. Although I haven’t actually reached the coding side of it, but the screens are all laid out and it’s been on the back of my mind since I thought I would need to validate more than one model.
Great write-up Jim. Looking forward to more gems like this from yourself.
You might want to have a look at the “presenter” pattern as well. Here is a nice implementation called ActivePresenter: http://jamesgolick.com/2008/7/28/introducing-activepresenter-the-presenter-library-you-already-know
Thanks for all the replies. I didn’t actually know you could do half the stuff proposed in the these comments, so thanks guys.
I’ll update the article later on today so reflect everything you chaps have said.
Kudos to you :)
Is there a nice way to do the same thing for the update method as well as create? I don’t see any alternative to update_attributes that doesn’t save the record…
There’s also another way in the Advanced Rails Recipe book for handling multiple-model in one form. It’s a technique written by Ryan Bates. You can go check it ‘em up too.
Scratch that. I should look at more than just ‘update’. This works just fine with attributes= in the update method.
Hai! That fail is not epic!
Very nice!
very helpfull, thanks
Yea after seeing this trickle across dzone the first thing that popped into my head was the presenter model that ol’ boy over at (http://jamesgolick.com/2008/7/28/introducing-activepresenter-the-presenter-library-you-already-know) did.
Glad I’m not the only one drinking the kool aid.
Question…
If all the save! calls are wrapped with a transaction, is running valid? on all models necessary?
I thought that if, for example, @dog isn’t valid, the save! call will throw an exception, and the whole transaction will be undone (ie. @person and @cat will be destroyed).
Am I wrong?