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.