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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This might help : http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html On 8/3/07, Jack W Jennings <karakoro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > Jack > > > > >-- Cheers! - Pratik http://m.onkey.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Aug 3, 12:02 pm, Pratik <pratikn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This might help :http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMet... > > On 8/3/07, Jack W Jennings <karak...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > > Jack > > -- > Cheers! > - Pratikhttp://m.onkey.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---