# Handling new form entries with relational tables

**URL:** <https://www.ruby-forum.com/t/handling-new-form-entries-with-relational-tables/72359>\
**Category:** Rails\
**Created:** [October 16, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/handling-new-form-entries-with-relational-tables/72359 "2006-10-16T18:41:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grasshoppa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e68b1a/32.png) [@Grasshoppa](https://www.ruby-forum.com/u/Grasshoppa)\
**Post date:** [October 16, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/handling-new-form-entries-with-relational-tables/72359/1 "2006-10-16T18:41:00Z")

</div>

Ok. I’m new at this, but let me try and explain my problem. I hope some  
generous soul will be kind enough to offer some guidance…

Let’s say I have a form that accepts new input from an applicant. I  
want to collect the applicant name (and other details) and a list of  
two references, (actually more, but I’m trying to keep it simple).  
Here’s a form I’ve created for this, (it’s called index.rhtml):

\<%= start\_form\_tag :action =\> ‘save\_applicant’ %\>

Applicant Info

Name: \<%= text\_field "applicant", "name", "size" =\> 20 %\>

Address: \<%= text\_field "applicant", "address", "maxsize" =\> 20 %\>

Reference Info

Ref Name 1: \<%= text\_field "ref1", "name", "size" =\> 20 %\>

Ref Email 1: \<%= text\_field "ref1", "email", "maxsize" =\> 20 %\>

```
<p>
Ref Name 2:
<%= text_field "ref2", "name", "size" => 20 %>
</p>
<p>
Ref Email 2:
<%= text_field "ref2", "email", "maxsize" => 20 %>
</p>

<input type="submit" value="Save">

```

\<%= end\_form\_tag %\>

Ok. I’ve got two models set up. An ‘applicant’ model and a ‘ref’ model.  
The applicant ‘has\_many’ refs and the ref ‘belongs\_to’ the applicant.  
I’ve got validation going on in both models…just  
‘validates\_presence\_of’ for all fields.

The controller I’m submitting to is called apply\_controller. It  
contains the following method:

def save\_applicant  
@applicant = Applicant.new(params[:applicant])  
if @applicant.save  
@applicant.refs.create(params[:ref1])  
@applicant.refs.create(params[:ref2])  
#go to an appropriate view to say ‘yeah, it worked’  
else  
render\_action “index” #redisplay the index page  
end  
end

This all works fine as far as getting the data into the database goes.  
But it does not validate the second model, i.e. the references. I can  
leave them blank and not get an validation message.

Anyone know of a way to get the reference model to validate the data.

Also, I’m almost positive that my methodology here is not the greatest.  
If you have any suggestions on a slicker ruby-esque way to accomplish  
this same thing, I’d love to hear it.

Thanks in advance to any takers! 🙂

---

<div class="post-metadata">

**Author:** ![Grasshoppa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e68b1a/32.png) [@Grasshoppa](https://www.ruby-forum.com/u/Grasshoppa)\
**Post date:** [October 17, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/handling-new-form-entries-with-relational-tables/72359/2 "2006-10-17T16:44:39Z")

</div>

Grasshoppa wrote:

> ```
> <p>
> Ref Name 1:
> </p>
> 
> ```
> 
> Ok. I’ve got two models set up. An ‘applicant’ model and a ‘ref’ model.  
> @applicant.refs.create(params[:ref1])
> 
> Anyone know of a way to get the reference model to validate the data.
> 
> Also, I’m almost positive that my methodology here is not the greatest.  
> If you have any suggestions on a slicker ruby-esque way to accomplish  
> this same thing, I’d love to hear it.

You’ll want something like:

def save\_applicant  
@applicant = Applicant.new(params[:applicant])  
@applicant.refs.build(params[:ref1])  
@applicant.refs.build(params[:ref2])  
if @applicant.save  
#go to an appropriate view to say ‘yeah, it worked’  
else  
render\_action “index” #redisplay the index page  
end  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Grasshoppa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e68b1a/32.png) [@Grasshoppa](https://www.ruby-forum.com/u/Grasshoppa)\
**Post date:** [October 18, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/handling-new-form-entries-with-relational-tables/72359/3 "2006-10-18T16:16:29Z")

</div>

Mark Reginald J. wrote:

> ```
> render_action "index" #redisplay the index page
> end
> 
> ```
> 
> end

Thanks for the reply Mark. I appreciate it. Using this approach leads  
to a couple of difficulties, though (at least for me).

The models and corresponding validations for my models look like this:

class Applicant \< ActiveRecord::Base

```
 has_many :refs

 validates_presence_of :name, :address, :message => "must be filled

```

in."

```
 validates_associated :refs

```

end

class Ref \< ActiveRecord::Base

belongs\_to :applicant

validates\_presence\_of :name, :email, :message =\> “fields must be  
filled in.”

end

Now when I submit the form with blank reference fields I get the  
following:

There were problems with the following fields:

```
* Refs is invalid
* Refs is invalid

```

I’ve experimented with a number of different things and haven’t been  
able to get the validations to do what I want them to do, which is  
either to display the validation message set in the Ref model or to  
display a customized validation method some other way.

I got the page to display a custom validation message by changing the  
Applicant model to include:

validates\_associated :refs, :message =\> “validation message from  
validates\_associated.”

The problem, though is that it also still includes the two ‘Refs is  
invalid’ messages.

In other words the result looks like this:

There were problems with the following fields:

```
* Refs message from Applicant v_a.
* Refs is invalid
* Refs is invalid

```

Any suggestions would be greatly appreciated! 🙂

---

<div class="post-metadata">

**Author:** ![Grasshoppa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e68b1a/32.png) [@Grasshoppa](https://www.ruby-forum.com/u/Grasshoppa)\
**Post date:** [October 18, 2006, 11:35pm UTC](https://www.ruby-forum.com/t/handling-new-form-entries-with-relational-tables/72359/4 "2006-10-18T23:35:35Z")

</div>

> The bottom two messages are from the validation check of  
> new associates that Rails automatically does on save of  
> new parents, so if you remove your validates\_associated  
> declaration you’ll just get these.

That’s interesting, I just ran into this today for the first time. Try  
as I might, I can’t find it documented anywhere either in the Agile  
Development with Ruby on Rails book  
(which seems to suggest in the “when things get saved” section that the  
opposite is true\*) or in the API docs. Perhaps my google-fu is weak  
today but would you happen to know where I could find more information  
on this feature?

Many thanks,  
G.

- “Finally, there’s a danger here. If the child row cannot be saved  
(for example,  
because it fails validation), Active Record will not  
complain-you’ll get no indi-  
cation that the row was not added to the database.”

---

<div class="post-metadata">

**Author:** ![Grasshoppa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e68b1a/32.png) [@Grasshoppa](https://www.ruby-forum.com/u/Grasshoppa)\
**Post date:** [October 18, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/handling-new-form-entries-with-relational-tables/72359/5 "2006-10-18T16:50:53Z")

</div>

Grasshoppa wrote:

> There were problems with the following fields:
> 
> ```
> * Refs message from Applicant v_a.
> * Refs is invalid
> * Refs is invalid
> 
> ```
> 
> Any suggestions would be greatly appreciated! 🙂

The bottom two messages are from the validation check of  
new associates that Rails automatically does on save of  
new parents, so if you remove your validates\_associated  
declaration you’ll just get these.

In order to fully customize your error messages you’ll  
have to check the validity of the triple, before you  
transactionally save the refs followed by the applicant.

Watch out for a bug in validates\_associated that causes it to  
stop validating an array of objects after the first invalid  
object, preventing the generation of error messages for other  
invalids.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Grasshoppa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e68b1a/32.png) [@Grasshoppa](https://www.ruby-forum.com/u/Grasshoppa)\
**Post date:** [October 19, 2006, 1:47am UTC](https://www.ruby-forum.com/t/handling-new-form-entries-with-relational-tables/72359/6 "2006-10-19T01:47:54Z")

</div>

Gavin wrote:

> today but would you happen to know where I could find more information  
> on this feature?  
> …
> 
> - “Finally, there’s a danger here. If the child row cannot be saved  
> (for example,  
> because it fails validation), Active Record will not  
> complain-you’ll get no indi-  
> cation that the row was not added to the database.”

The automatic pre-save validation of new associates is only for  
has\_many and has\_and\_belongs\_to\_many associations, and is not  
to my knowledge properly documented. Here’s the part of the core  
source where it’s done:

[http://dev.rubyonrails.org/browser/tags/rel\_1-1-6/activerecord/lib/active\_record/associations.rb#L908](http://dev.rubyonrails.org/browser/tags/rel_1-1-6/activerecord/lib/active_record/associations.rb#L908)

Your extract from the Agile book is referring to has\_one associations,  
which try to validate and save their associates /after/ the parent  
has been saved.

Like has\_many & habtm, belongs\_to associations try to validate and save  
new associates /before/ trying to save the parent, but unlike has\_many  
and habtm if the associate fails validation an attempt will still be  
made  
to save the parent.

So there is some inconsistency between the different association types  
with regards to automatic validation.

Probably the best thing to do would be change the core code so  
that any automatic associate validation is only done if the  
associate is clear of errors, preventing your problem of a second  
validation adding unwanted error messages.

–  
We develop, watch us RoR, in numbers too big to ignore.
