views:

42

answers:

1

I'm developing the Order Model for a Rails application.I'm trying to represent an Order which has BillToAddressId and ShipToAddressId as the foreign keys from the Address table.

The address table is below :

create_table :addresses do |t|
      t.string :country
      t.string :state
      t.string :city
      t.string :zipcode
      t.string :line1
      t.timestamps

I'm a Rails newbie, so I'm not sure how to represent this in the DB/migrate for Order and Address.

It would be great if someone can guide me to build the Model and migrate script.

+3  A: 

You can do it in follwing way

class Order < ActiveRecord::Base
  belongs_to :bill_to, :class_name => 'Address', :foreign_key => 'BillToAddressId'
  belongs_to :ship_to, :class_name => 'Address', :foreign_key => 'ShipToAddressId'
end

For Ruby Naming Conevention Column name "ShipToAddressId" must be "ship_to_address_id"

Salil
Thank you Salil, Will try this out.