Hello, I am trying to write a sample application. I have class LineItem< ActiveRecord::Base belongs_to : order end && class Order < ActiveRecord::Base has_one :line_item end Now in the controller i am trying : def add @ord = Order.new(params[:ord]) @ord.line_item = LineItem.new(params[:lineitem]) if @ord.save redirect_to :action => ''list'' else render :action => ''new'' end end and that is giving an exception saying foreign key constraint fails on line_item because the order_id is 0 and more over the order is not saved ! any help ? -- Posted via http://www.ruby-forum.com/.
pleaase ? -- Posted via http://www.ruby-forum.com/.
Don''t you think that your order should have many LineItems? Like class Order < AR::Base has_many :line_items end class LineItem < AR::Base belongs_to :order end now order = Order.new @params[:order] li = LineItem.new @params[:line_item] order.line_items << li order.save! li.save! -- Kent --- http://www.datanoise.com On 2/15/06, Satya <j2ee_satya@yahoo.com> wrote:> Hello, > > > I am trying to write a sample application. > > I have > > class LineItem< ActiveRecord::Base > belongs_to : order > end > > && > > class Order < ActiveRecord::Base > has_one :line_item > end > > Now in the controller i am trying : > > def add > > @ord = Order.new(params[:ord]) > @ord.line_item = LineItem.new(params[:lineitem]) > if @ord.save > redirect_to :action => ''list'' > else > render :action => ''new'' > end > > end > > > > and that is giving an exception saying foreign key constraint fails on > line_item because the order_id is 0 and more over the order is not saved
order and line_items are just names that i was using... i was actually trying the basics with dealing records on ruby. i was trying one-one relationship. and that didnt work ! why was i getting that foreign-key constraint error ? Wont ruby insert the order and line_item when i do a @ord.save? -- Posted via http://www.ruby-forum.com/.
its a simple question : when order is saved would line_item be saved or should i save them seperately ? if they are supposed to be save automatically can someone tell me how ? assuming @ord contains the order. @line_item contains the line_item. Thanks -- Posted via http://www.ruby-forum.com/.
Hm, I''ve just created the same models as you are using and them work as expected. Strange, really. Can you check what sql statements get executed in your log file? Kent --- http://www.datanoise.com On 2/16/06, Satya <j2ee_satya@yahoo.com> wrote:> its a simple question : > > when order is saved would line_item be saved or should i save them > seperately ? > > if they are supposed to be save automatically can someone tell me how ? > > assuming @ord contains the order. > > @line_item contains the line_item. > > Thanks > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
unfortunately my log file is not associated... theres some error with that.. i have to fix that tooo :( -- Posted via http://www.ruby-forum.com/.
well... well.... its a typo that was causing this error.... foreign key constraint fails !! in the line_item table i had a orders_id column instead of order_id.... i changed that and it worked fine just like yours !! I wonder how one could fix that.... Is there a way that i can tell ruby , dont use your imagination, just work as told ? :) I mean, just like i say LineItem belongs_to Order can i also say LineItem belongs_to Order via orders_id ? -- Posted via http://www.ruby-forum.com/.
Kent Sibilev
2006-Feb-16 05:43 UTC
[Rails] Re: Re: association not inserted automatically.
On 2/16/06, Satya <j2ee_satya@yahoo.com> wrote:> well... well.... > can i also say > > LineItem > belongs_to Order via orders_id ? >Yes. class LineItem < AR::Base belongs_to :order, :foreign_key => ''orders_id'' end Kent --- http://www.datanoise.com