This seems simple enough, but it''s stumped the hell out of me, so I''m seeking assistance from you fine folks. I keep getting ActiveRecord::StatementInvalid when trying to add a new record to my database. I''ve narrowed it down to which relationship is causing the error, but I can''t figure out why. The actual error generated is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' at line 1: INSERT INTO quote_shipments (`box`, `quote_id`, `carrier_id`, `weight`, `date_received`, `user_id`, `tracking_no`) VALUES(#<Quote:0x40ac90a0>, #<Quote:0x40a81a5c>, #<Quote:0x409fde50>, #<Quote:0x40a3bfd4>, #<Quote:0x409933c0>, #<Quote:0x4094daf0>, #<Quote:0x40923aac>) Here are the relevant models Quote has_many :quote_shipments Carrier has_many :quote_shipments QuoteShipment belongs_to :carrier AND belongs_to :quote The error occurs when I add a new QuoteShipment. If I remove "belongs_to :quote", the record will insert fine. In fact I can still access the records via @quote.quote_shipments I have the quote_id column in my quote_shipments table, and an id column in quotes, so what could be causing this? How does this relationship change the sql that''s generated by ActiveRecord? I was under the impression both models needed to express the relationships to each other, am I wrong? Is something gonna jump up and bite me if I leave off "belongs_to :quote" from the QuoteShipment model. (aside from not being able to access info via @quote_shipments.quote) I don''t want to fill this already lengthy mail with unecessary details, so I''ll refrain from posting all the schemas/versions, but can if necessary. I have read, and re-read, the relationships section in the Agile book, but found no light. If I''m just missing something, please point me to the page. Thanks, -- Chris Martin Web Developer Open Source & Web Standards Advocate http://www.chriscodes.com/
On 9/7/05, Chris Martin <chriscodes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This seems simple enough, but it''s stumped the hell out of me, so I''m > seeking assistance from you fine folks. > > I keep getting ActiveRecord::StatementInvalid when trying to add a > new record to my database. I''ve narrowed it down to which relationship > is causing the error, but I can''t figure out why. > > The actual error generated is: > You have an error in your SQL syntax; check the manual that > corresponds to your MySQL server version for the right syntax to use > near '''' at line 1: INSERT INTO quote_shipments (`box`, `quote_id`, > `carrier_id`, `weight`, `date_received`, `user_id`, `tracking_no`) > VALUES(#<Quote:0x40ac90a0>, #<Quote:0x40a81a5c>, #<Quote:0x409fde50>, > #<Quote:0x40a3bfd4>, #<Quote:0x409933c0>, #<Quote:0x4094daf0>, > #<Quote:0x40923aac>) > > Here are the relevant models > Quote has_many :quote_shipments > Carrier has_many :quote_shipments > QuoteShipment belongs_to :carrier AND belongs_to :quote > > The error occurs when I add a new QuoteShipment. If I remove > "belongs_to :quote", the record will insert fine. In fact I can still > access the records via @quote.quote_shipments > > I have the quote_id column in my quote_shipments table, and an id > column in quotes, so what could be causing this? > How does this relationship change the sql that''s generated by ActiveRecord? > I was under the impression both models needed to express the > relationships to each other, am I wrong? > Is something gonna jump up and bite me if I leave off "belongs_to > :quote" from the QuoteShipment model. (aside from not being able to > access info via @quote_shipments.quote) > > I don''t want to fill this already lengthy mail with unecessary > details, so I''ll refrain from posting all the schemas/versions, but > can if necessary. > I have read, and re-read, the relationships section in the Agile book, > but found no light. If I''m just missing something, please point me to > the page. > > Thanks, >Anyone?... Anyone?... ...Bueler? -- Chris Martin Web Developer Open Source & Web Standards Advocate http://www.chriscodes.com/
Hi, I ''m a new bie also , this is only my opinion. it seems the insert object doesn''t have the right data , ibecause all the values look like an instance object instead. it might miss an attribute. if you can put the code on relevant controller and view class I can try to look and see if I can spot the question. Thanks --------------------------------- DO YOU YAHOO!? 雅虎免费G邮箱-中国第一绝无垃圾邮件骚扰超大邮箱 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 9/7/05, s c <inno_hub-/E1597aS9LRv1O+Z8WTAqQ@public.gmane.org> wrote:> Hi, I ''m a new bie also , this is only my opinion. > it seems the insert object doesn''t have the right data , ibecause all the > values look like an instance object instead. it might miss an attribute. > if you can put the code on relevant controller and view class I can try to > look and see if I can spot the question. > ThanksThat was my first assumption, and I went over the view, and controller till my eyes bled. Even tried every conceivable way of adding the record in the controller (eg, QuoteShipment.new() then .save, QuoteShipment.create(), @quote.quote_shipments << @params[:quote_shipment], even setting every attribute manually) When I comment out the save portion, and debug() the object I''m about to save, everything checks out, all fields, all data. I even posted the view to a debug action with debug(@params[:quote_shipment]) And verified the fields against the database, so at this point I''m fairly confident it''s not a view/controller issue, but something with my model (or my database) That doesn''t make sense to me, because I''m only adding data to one table, but everything works as expected when I remove "belongs_to :quote" from the QuoteShipment model But, I''ll post the code anyway in just a few, maybe I''ve missed something silly. -- Chris Martin Web Developer Open Source & Web Standards Advocate http://www.chriscodes.com/
On 9/7/05, Chris Martin <chriscodes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: <snip>> But, I''ll post the code anyway in just a few, maybe I''ve missed something silly. >Here''s the code for the controller actions, and view. One minor detail is that although I''m adding a QuoteShipment, I''m doing it from the Quotes controller. I don''t think that should matter though ######### Controller ########## # this makes use of the view def receive_package @quote = Quote.find(@params[:id]) @carriers = Carrier.find(:all) @quote_shipment = QuoteShipment.new() end # this creates the new record def update_shipment @quote = Quote.find(@params[:id]) @quote_shipment = QuoteShipment.new(@params[:quote_shipment]) if @quote_shipment.save flash[''notice''] = ''Shipping details added successfully!'' end redirect_to :action => ''receive_package'', :id => @params[:id] end ############ View ############# <%= start_form_tag :action => ''update_shipment'', :id => @quote %> <div id="quoteShipmentInfo"> <p><label for="quote_carrier">Carrier</label> <select name="quote_shipment[carrier_id]" id="quote_shipment_carrier_id"> <%= options_from_collection_for_select(@carriers, ''id'', ''name'') %> </select> </p> <p><label for="quote_shipment_tracking_no">Tracking number</label> <%= text_field ''quote_shipment'', ''tracking_no'' %></p> <p><label for="quote_shipment_box">Type</label> <select name="quote_shipment[box]" id="quote_shipment_box"> <option value="0">Pallet</option> <option value="1">Box</option> </select> </p> <p><label for="quote_shipment_weight">Weight</label> <%= text_field ''quote_shipment'', ''weight'', {:size => ''5''} %></p> <%= hidden_field ''quote_shipment'', ''date_received'', {:value => Time.now} %> <%= hidden_field ''quote_shipment'', ''user_id'', {:value => session[:user][:id]} %> <%= hidden_field ''quote_shipment'', ''quote_id'', :value => @quote.id %> <%= submit_tag "Receive Quote" %> </div> <%= end_form_tag %> ############ End Code ########### Tell me it''s something silly I''ve overlooked. :) Thanks, Chris Martin Web Developer Open Source & Web Standards Advocate http://www.chriscodes.com/
On 7.9.2005, at 19.43, Chris Martin wrote:> On 9/7/05, Chris Martin <chriscodes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> This seems simple enough, but it''s stumped the hell out of me, so I''m >> seeking assistance from you fine folks. >> >> I keep getting ActiveRecord::StatementInvalid when trying to add a >> new record to my database. I''ve narrowed it down to which >> relationship >> is causing the error, but I can''t figure out why. >> >> The actual error generated is: >> You have an error in your SQL syntax; check the manual that >> corresponds to your MySQL server version for the right syntax to use >> near '''' at line 1: INSERT INTO quote_shipments (`box`, `quote_id`, >> `carrier_id`, `weight`, `date_received`, `user_id`, `tracking_no`) >> VALUES(#<Quote:0x40ac90a0>, #<Quote:0x40a81a5c>, #<Quote:0x409fde50>, >> #<Quote:0x40a3bfd4>, #<Quote:0x409933c0>, #<Quote:0x4094daf0>, >> #<Quote:0x40923aac>)For some reason all the fields are being populated (or tried at least) with Quote objects. So it seems there''s something pretty borked in your code. Could you post the whole code that does this insert?>> I have the quote_id column in my quote_shipments table, and an id >> column in quotes, so what could be causing this? >> How does this relationship change the sql that''s generated by >> ActiveRecord? >> I was under the impression both models needed to express the >> relationships to each other, am I wrong? >> Is something gonna jump up and bite me if I leave off "belongs_to >> :quote" from the QuoteShipment model. (aside from not being able to >> access info via @quote_shipments.quote)No, they''re just for your convenience. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 9/8/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > For some reason all the fields are being populated (or tried at least) with > Quote objects. So it seems there''s something pretty borked in your code. > Could you post the whole code that does this insert? >I did. http://article.gmane.org/gmane.comp.lang.ruby.rails/21213 But it seems gmane replaces "at" symbols with "<at>", so here it is again. ######### Controller ########## # this makes use of the view def receive_package @quote = Quote.find(@params[:id]) @carriers = Carrier.find(:all) @quote_shipment = QuoteShipment.new() end # this creates the new record def update_shipment @quote = Quote.find(@params[:id]) @quote_shipment = QuoteShipment.new(@params[:quote_shipment]) if @quote_shipment.save flash[''notice''] = ''Shipping details added successfully!'' end redirect_to :action => ''receive_package'', :id => @params[:id] end ############ View ############# <%= start_form_tag :action => ''update_shipment'', :id => @quote %> <div id="quoteShipmentInfo"> <p><label for="quote_carrier">Carrier</label> <select name="quote_shipment[carrier_id]" id="quote_shipment_carrier_id"> <%= options_from_collection_for_select(@carriers, ''id'', ''name'') %> </select> </p> <p><label for="quote_shipment_tracking_no">Tracking number</label> <%= text_field ''quote_shipment'', ''tracking_no'' %></p> <p><label for="quote_shipment_box">Type</label> <select name="quote_shipment[box]" id="quote_shipment_box"> <option value="0">Pallet</option> <option value="1">Box</option> </select> </p> <p><label for="quote_shipment_weight">Weight</label> <%= text_field ''quote_shipment'', ''weight'', {:size => ''5''} %></p> <%= hidden_field ''quote_shipment'', ''date_received'', {:value => Time.now} %> <%= hidden_field ''quote_shipment'', ''user_id'', {:value => session[:user][:id]} %> <%= hidden_field ''quote_shipment'', ''quote_id'', :value => @quote.id %> <%= submit_tag "Receive Quote" %> </div> <%= end_form_tag %> ############ End Code ########### Remember, this is what seems so strange to me... When I comment out the portion in update_shipment that does the save, and place the following in a template: debug(@quote_shipment) debug(@params[:quote_shipment]) Everything appears as I would expect, but for some reason it throws that error when I save. I''ve tried all variations of saving it (.new(), .create(), <<), all behave the same. When I take away the relationship to the Quote model from the QuoteShipment, it inserts fine. Thanks for taking a look! -- Chris Martin Web Developer Open Source & Web Standards Advocate http://www.chriscodes.com/