# Validation for multiple tables

**URL:** <https://www.ruby-forum.com/t/validation-for-multiple-tables/225961>\
**Category:** Rails\
**Created:** [August 3, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/validation-for-multiple-tables/225961 "2007-08-03T20:24:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_W\_Jennings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba8739/32.png) [@Jack\_W\_Jennings](https://www.ruby-forum.com/u/Jack_W_Jennings)\
**Post date:** [August 3, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/validation-for-multiple-tables/225961/1 "2007-08-03T20:24:48Z")

</div>

I have a single for that needs to save to separate tables in my  
database, and create referential IDs for my has\_many and belongs\_to  
models (everything belongs\_to the ‘entry’). The code that I have  
written is below. WARNING: it’s ugly.

I need to validate the presence of each of these forms, thus the  
terribly long line that checks for all of the saving. However, the  
problem with this, is that if something fails along the line, the  
other items have already been saved. Not what I want to happen.

Is there a way to simultaneously save and check validations for fields  
connected to multiple tables? Is there an easier way to do this in  
general? I have a feeling that I don’t get how foreign keys are  
supposed to function (I’m new to Ruby and application programing in  
general).

app/controllers/entry\_controller.rb

def index  
# …  
return unless request.post?  
@entry = Entry.new(params[:entry])  
@route = Route.new(params[:route])  
@destination = Destination.new(params[:destination])  
@entry\_date = EntryDate.new(params[:entry\_date])  
@person = Person.new(params[:person])  
if @entry.save && @route.save && @destination.save &&  
@entry\_date.save && @person.save  
@route.update\_attribute(“entry\_id”, @entry.id)  
@destination.update\_attribute(“entry\_id”, @entry.id)  
@entry\_date.update\_attribute(“entry\_id”, @entry.id)  
@person.update\_attribute(“entry\_id”, @entry.id)  
flash[:notice] = ‘Entry was successfully created.’  
redirect\_to :action =\> ‘index’  
else  
render :action =\> ‘index’  
end  
end

app/views/entry/index.rhtml

\<% form\_tag :action =\> ‘index’ do %\>

\<%= text\_field ‘entry\_date’, ‘when’ %\>  
\<%= text\_field ‘route’, ‘how’ %\>  
\<%= text\_field ‘destination’, ‘where’ %\>  
\<%= text\_field ‘person’, ‘what’ %\>  
\<%= text\_area ‘entry’, ‘body’ %\>

\<%= submit\_tag “Create” %\>  
\<% end %\>

Thank you for any help in advance. I’ve been running my head into this  
for far too long, and it seems like it should be a lot simpler than it  
has turned out to be.

Jack

---

<div class="post-metadata">

**Author:** ![Jack\_W\_Jennings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba8739/32.png) [@Jack\_W\_Jennings](https://www.ruby-forum.com/u/Jack_W_Jennings)\
**Post date:** [August 3, 2007, 9:02pm UTC](https://www.ruby-forum.com/t/validation-for-multiple-tables/225961/2 "2007-08-03T21:02:53Z")

</div>

This might help :

> **[ActiveRecord::Transactions::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html)**
>
> Active Record Transactions Transactions are protective blocks where SQL statements are only permanent if they can all succeed as one atomic action.

On 8/3/07, Jack W Jennings [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> return unless request.post?
> @person.update_attribute("entry_id", @entry.id)
> 
> ```
> 
> for far too long, and it seems like it should be a lot simpler than it  
> has turned out to be.
> 
> Jack
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)

---

<div class="post-metadata">

**Author:** ![Jack\_W\_Jennings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba8739/32.png) [@Jack\_W\_Jennings](https://www.ruby-forum.com/u/Jack_W_Jennings)\
**Post date:** [August 4, 2007, 1:05am UTC](https://www.ruby-forum.com/t/validation-for-multiple-tables/225961/3 "2007-08-04T01:05:24Z")

</div>

Jack W Jennings wrote:

> Is there a way to simultaneously save and check validations for fields  
> @entry = Entry.new(params[:entry])  
> flash[:notice] = ‘Entry was successfully created.’  
> \<%= text\_field ‘entry\_date’, ‘when’ %\>  
> has turned out to be.  
> Try the valid? method as in something like:

if @entry.valid? && @route.valid? && …  
@entry.save  
@route.save  
…  
end

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![Jack\_W\_Jennings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ba8739/32.png) [@Jack\_W\_Jennings](https://www.ruby-forum.com/u/Jack_W_Jennings)\
**Post date:** [August 3, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/validation-for-multiple-tables/225961/4 "2007-08-03T21:30:56Z")

</div>

Now I have:

def index  
@entries = Entry.find( :all )  
return unless request.post?  
if simple\_captcha\_valid?  
@entry = Entry.new(params[:entry])  
@route = Route.new(params[:route])  
@destination = Destination.new(params[:destination])  
@entry\_date = EntryDate.new(params[:entry\_date])  
@person = Person.new(params[:person])  
Entry.transaction() do  
@entry.save\_with\_validation  
@route.save\_with\_validation  
@destination.save\_with\_validation  
@entry\_date.save\_with\_validation  
@person.save\_with\_validation  
@route.update\_attribute(“entry\_id”, @entry.id)  
@destination.update\_attribute(“entry\_id”, @entry.id)  
@entry\_date.update\_attribute(“entry\_id”, @entry.id)  
@person.update\_attribute(“entry\_id”, @entry.id)  
end  
redirect\_to :action =\> ‘index’  
else  
flash[:notice] = ‘Image and text did not match.’  
render :action =\> ‘index’  
end  
end

And everything posts… even with empty fields. No validation occurs  
at all.

Jack
