mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-01 04:37 UTC
model relationship sanity check please
I tried to post this earlier but it didn''t show? Happened a couple
times now.
I hate having to ask this but my eyeballs are spinning and I seem to be
getting some odd behavior.
a bit of the schema:
create_table "orders", :force => true do |t|
t.column "billing_address_id", :integer
...
end
# single table inheritance here... AddressBilling & AddressShipping
create_table "addresses", :force => true do |t
t.column "type", :string, :limit => 20
....
end
which of these pairs is correct?
class Order < ActiveRecord::Base
has_one :billing_address,
:class_name => "AddressBilling"
end
class AddressBilling < Address
belongs_to :order
end
------- OR ----------------
class Order < ActiveRecord::Base
belongs_to :billing_address,
:class_name => "AddressBilling"
end
class AddressBilling < Address
has_one :order
end
I thought the first was the correct but I''m a noob and neither seem to
be working as I''d expect.
When I do this in the controller:
@orders = Order.find(:all)
then in the view:
<% @orders.each do |order| %>
<tr><td><%= order.billing_address_id %></td></tr>
<% end %>
that works fine but then if I do this:
<% @orders.each do |order| %>
<tr><td><%= order.billing_address.id %></td></tr>
<% end %>
I get an error about billing address being a nil object? Yes the
billing address is there with the correct id.
seems odd to me... any help greatly appreciated and thanks in advance!
Tim
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---