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 ?
on 2006-02-16 05:03
on 2006-02-16 05:11
on 2006-02-16 05:23
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 2006-02-16 05:27
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?
on 2006-02-16 06:00
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
on 2006-02-16 06:05
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 2006-02-16 06:33
unfortunately my log file is not associated... theres some error with that.. i have to fix that tooo :(
on 2006-02-16 06:38
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 ?
on 2006-02-16 06:45
On 2/16/06, Satya <firstname.lastname@example.org> 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
on 2006-02-16 06:48
Thanks man... Appreciate your help.