Hi Philip - Thanks for responding.  Yeah, I thought about what happens
when they go to change an old address.  Not sure how to handle it at
this point.  I did find a plugin written by Ryan Bates that at least
handled "freezing" an association.

http://github.com/ryanb/association-freezer/blob/4691b3247332ac9a5d534206e80080a54ff0a48d/association-freezer.gemspec

Seems like an interesting piece of code and I'll have to learn more
about it.

I see that you ran a test but I noticed that you used:

> class Customer < ActiveRecord::Base
>    belongs_to :preferred_billing_address, :class_name => "Address"
>    belongs_to :preferred_shipping_address, :class_name => "Address"
>    has_many :orders
> end

What happens if you use has_many instead of belongs_to?  It just makes
more sense to me that a Customer can have many addresses regardless of
whether they are of type shipping or billing.



On Nov 22, 8:50 am, Philip Hallstrom <[email protected]> wrote:
> > On Sat, Nov 21, 2009 at 8:12 PM, Philip Hallstrom <[email protected]>  
> > wrote:
>
> >>> Order
> >>>   belongs_to :customer
> >>>   belongs_to :billing_address, :class_name => 'Address'
> >>>   belongs_to :shipping_address, :class_name => 'Address'
> >>> ....snip....
> >>> 1) in the OrdersController
>
> >>>     def new
> >>>         �...@order = current_user.orders.build(
> >>>                               :billing_address =>
> >>> current_user.preferred_billing_address,
> >>>                               :shipping_address =>
> >>> current_user.preferred_shipping_address
> >>>                         )
> >>>        #...
> >>>      end
>
> >> One thing that popped into my head while reading this is that given
> >> the above, if I update my shipping/billing address, any orders I've
> >> previously placed will have their addresses updated as well.
>
> > No they won't.
>
> > When the order is saved, then it will have a shipping_address_id and
> > billing_address_id set to either the values intialized by the
> > controller in the new method (which is used as a template for the
> > values posted back to the create method by the form) or whatever the
> > user changed it to on the order form.
>
> > If the user changes his preferences later, it won't affect existing  
> > orders.
>
> > I think you might be better off just to write some code and test it
> > and see what happens, rather than continuing with what seem to be
> > gedanken experiments.
>
> I think you misunderstood or I didn't make it clear...  if the user  
> changes his preferred address, everything will be fine.  If there is  
> functionality that lets a user update his old addresses then there  
> will be a problem in that the order will be updated too.  Say the user  
> ships things to his work address.  Then he changes jobs and updates  
> his work address... any order that had a shipping address of that same  
> id will now have the new address, not the old.
>
> class Address < ActiveRecord::Base
> end
> class Customer < ActiveRecord::Base
>    belongs_to :preferred_billing_address, :class_name => "Address"
>    belongs_to :preferred_shipping_address, :class_name => "Address"
>    has_many :orders
> end
> class Order < ActiveRecord::Base
>    belongs_to :customer
>    belongs_to :billing_address, :class_name => 'Address'
>    belongs_to :shipping_address, :class_name => 'Address'
> end
>
>    create_table "addresses", :force => true do |t|
>      t.string   "street"
>      t.datetime "created_at"
>      t.datetime "updated_at"
>    end
>
>    create_table "customers", :force => true do |t|
>      t.string   "name"
>      t.integer  "preferred_billing_address_id"
>      t.integer  "preferred_shipping_address_id"
>      t.datetime "created_at"
>      t.datetime "updated_at"
>    end
>
>    create_table "orders", :force => true do |t|
>      t.integer  "customer_id"
>      t.integer  "billing_address_id"
>      t.integer  "shipping_address_id"
>      t.datetime "created_at"
>      t.datetime "updated_at"
>    end
>
>  >> c = Customer.create(:name => 'Philip')
>  >> a1 = Address.create(:street => '123 Maple St')
>  >> a2 = Address.create(:street => '456 Syrup St')
>  >> c.preferred_shipping_address = a1
>  >> c.preferred_billing_address = a2
>  >> c.save
>  >> o = c.orders.build(:shipping_address =>  
> c.preferred_shipping_address,
>                        :billing_address => c.preferred_billing_address)
>  >> c.preferred_shipping_address.street
> => "123 Maple St"
>  >> c.preferred_billing_address.street
> => "456 Syrup St"
>  >> o.shipping_address.street
> => "123 Maple St"
>  >> o.billing_address.street
> => "456 Syrup St"
>  >> a1.street = '789 Blueberry St'
> => "789 Blueberry St"
>  >> a1.save
>  >> c.preferred_shipping_address.street
> => "789 Blueberry St"
>  >> o.shipping_address.street
> => "789 Blueberry St"

--

You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to