# Problem saving parent and children using belongs\_to, class\_n

**URL:** <https://www.ruby-forum.com/t/problem-saving-parent-and-children-using-belongs-to-class-n/78720>\
**Category:** Rails\
**Created:** [December 8, 2006, 12:39am UTC](https://www.ruby-forum.com/t/problem-saving-parent-and-children-using-belongs-to-class-n/78720 "2006-12-08T00:39:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 8, 2006, 12:39am UTC](https://www.ruby-forum.com/t/problem-saving-parent-and-children-using-belongs-to-class-n/78720/1 "2006-12-08T00:39:28Z")

</div>

Hi, I have a real simple association setup here that’s just trying to:

- Create a new PayjunctionOrder
- Create some LineItem objects and assign them to the PayjunctionOrder
- Save everything

I’m using Rails Edge.

* * *

class PayjunctionOrder \< ActiveRecord::Base  
has\_many :line\_items

## end

* * *

## class LineItem \< ActiveRecord::Base belongs\_to :order, :class\_name =\> “PayjunctionOrder”, :foreign\_key =\> “order\_id” …

* * *

class StoreController \< ApplicationController

def checkout\_complete  
…  
@order = PayjunctionOrder.new  
@order.line\_items \<\< LineItem.new(:product =\> product, :quantity =\>  
qty, :total\_price =\> product.price \* qty)

## @order.save! …

When I try to save, I get:

Mysql::Error: #23000Column ‘order\_id’ cannot be null: INSERT INTO  
line\_items (`order_id`, `total_price`, `product_id`, `quantity`)  
VALUES(NULL, ‘285.0’, 5, 1)

The development.log shows the insert being done for the  
PayjunctionOrder, but the inserts for the children fail on this NOT  
NULL constraint. So for whatever reason, order\_id isn’t being set in  
the children.

What’s weird is that it will work just fine (as advertised) by renaming  
the column “order\_id” to “payjunction\_order\_id” in the line\_items table  
and doing this instead:

* * *

## class LineItem \< ActiveRecord::Base belongs\_to :payjunction\_order …

However, this isn’t an optimal solution.

So it seems like maybe Rails is ignoring the :class\_name and  
:foreign\_key options in belongs\_to. Does anyone have any insight as to  
whats going on here? Thanks for the help.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 8, 2006, 12:49am UTC](https://www.ruby-forum.com/t/problem-saving-parent-and-children-using-belongs-to-class-n/78720/2 "2006-12-08T00:49:29Z")

</div>

Here are the table schemas for reference:

create\_table “line\_items”, :force =\> true do |t|  
t.column “product\_id”, :integer,  
:default =\> 0, :null =\> false  
t.column “order\_id”, :integer,  
:default =\> 0, :null =\> false  
t.column “quantity”, :integer,  
:default =\> 0, :null =\> false  
t.column “total\_price”, :decimal, :precision =\> 8, :scale =\> 2,  
:default =\> 0.0, :null =\> false  
end

create\_table “payjunction\_orders”, :force =\> true do |t|  
t.column “total\_amount”, :decimal, :precision =\> 8, :scale =\>  
2, :default =\> 0.0, :null =\> false  
t.column “response\_code”, :string, :limit =\> 5, :default =\> “”,  
:null =\> false  
t.column “response\_message”, :string  
t.column “tracking\_code”, :string, :limit =\> 15  
t.column “cc\_last\_four”, :string, :limit =\> 22  
t.column “first\_name”, :string, :limit =\> 30  
t.column “last\_name”, :string, :limit =\> 20  
t.column “email”, :string, :limit =\> 30  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 8, 2006, 8:05am UTC](https://www.ruby-forum.com/t/problem-saving-parent-and-children-using-belongs-to-class-n/78720/3 "2006-12-08T08:05:58Z")

</div>

I finally got this sorted, so here’s the solution for anyone else with  
this problem.

Seems that the parent class also needs to know what the foreign key is  
called in the child. So, you’d need the following:

* * *

class PayjunctionOrder \< ActiveRecord::Base  
has\_many :line\_items, :foreign\_key =\> “order\_id”

## end

I guess it makes sense, but it seems kinda redundant to me. Also, it’s  
not explicitly mentioned in the docs from what I could see; I really  
think it needs to be. Nudge-nudge to the Rails Gods.
