Validation for multiple tables

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

This might help : http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

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

Jack W Jennings wrote:

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.

Try the valid? method as in something like:

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