Problem with Complex Form Saving

I'm having a problem with a fairly complex form, with data from multiple

I've trimmed down the code to the bare minimum ...

=========== START OF MODELS =============
class Order < ActiveRecord::Base
   has_many :order_products
   has_many :card_transactions
   belongs_to :purchaser, :class_name => 'Party', :foreign_key
=> "purchaser_party_id"
   belongs_to :recipient, :class_name => 'Party', :foreign_key
=> "recipient_party_id"

class Party < ActiveRecord::Base
    has_many :purchaser_orders, :class_name => 'Order', :foreign_key
=> 'purchaser_party_id'
    has_many :recipient_orders, :class_name => 'Order', :foreign_key
=> 'recipient_party_id'

class OrderProduct < ActiveRecord::Base
        belongs_to :order
        belongs_to :product

class CardTransaction < ActiveRecord::Base
    belongs_to :order
========== END OF MODELS ====================

You'll notice that I have two references to Party from Order : the purchaser
and the recipient.

Now here is the controller -

========== START OF CONTROLLER ==================
class OrdersController < ApplicationController
  def new
    @orders =
    @purchaser = @orders.build_purchaser
    @recipient = @orders.build_recipient
    1.times { }
    @card_transaction =
def create
    params[:orders][:last_updated_timestamp] =
    params[:orders][:last_changed_user_id] = '100'
    params[:orders][:order_status_id] =
OrderStatus.find(:first, :conditions => ["order_status_desc = ?", 'Phone
    params[:orders][:account_id] = '100'
    if !params[:delivery_time_before].nil? then
      params[:orders][:delivery_time_before] = params[:delivery_time_before]
[:hour_minute] + ":00"
    if !params[:delivery_time_after].nil? then
      params[:orders][:delivery_time_after] = params[:delivery_time_after]
[:hour_minute] + ":00"

    @orders =[:orders])
    @orders.wire_service_id = params[:wire_service_id]

    params[:card_transaction][:transaction_timestamp] =

    # @purchaser = @orders.build_purchaser(params[:purchaser])
    # @recipient = @orders.build_recipient(params[:recipient])

    @card_transaction =[:card_transaction])
    @purchaser =[:purchaser])
    @recipient =[:recipient])

    @orders.transaction do
      params[:orders][:purchaser_party_id] =
      params[:orders][:recipient_party_id] =

       if !
         render :action => 'new'

       @card_transaction.order_id =
       @card_transaction.transaction_amount = @orders.order_price_total
       @card_transaction.credit_card_type_id = params[:credit_card_type_id]
         flash[:notice] = 'Order ' + String( + ' was successfully
         redirect_to :action => 'queue'
         render :action => 'new'
================== END OF CONTROLLER ===================

When I call the create method, I get records in ORDERS, CARD_TRANSACTIONS and
ORDER_PRODUCTS. But no records in PARTIES and the recipient_party_id and
purchaser_party_id in ORDERS are both null.

I'm totally lost !!!

Any help gladly appreciated.


    @orders.transaction do

This is what will be messing you up I think.

Wouldn't it be "Order.transaction do..."


Also, a transaction will only fail and roll back if an exception is
raised within the block, none of your .save commands are using .save!
so they will return false, not raise an exception if they fail. You
might want to change this.



Unfortunately that isn't the problem, although what you have suggested is

I know about save! and was intending to change that.

When I did this, it started to give me clues -

ActiveRecord::RecordInvalid (Validation failed: Last name can't be blank):

The fact that the column is "surname" and the model validation has "last_name"
was the problem there !!!

Anyway, the party records are now being saved ... but I still was not getting
the IDs into Orders.

Then I noticed that I was loading up the params[:orders] AFTER I'd loaded the
@orders local variable with this. Changing -

params[:orders][:purchaser_party_id] =


@orders.purchaser_party_id =

and everything now works !!!

Thanks for the pointers.