mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-01 04:21 UTC
sanity check on setting up relationships please
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 -~----------~----~----~----~------~----~------~--~---
mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-01 04:25 UTC
sanity check on setting up relationships please
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 -~----------~----~----~----~------~----~------~--~---
mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-01 05:48 UTC
sanity check on setting up relationships please
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 -~----------~----~----~----~------~----~------~--~---
On 12/1/06, mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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.The second one is correct. While "Order HAS ONE billing address" seems to read more logically, keep in mind that the has_one, belongs_to stuff is really only there to tell AR about the relationships, not to be semantically correct. Think of it in this way: does the order need to know about its billing address, or does a billing address need to know about its order? The answer should be the former - an order needs to know where it should be shipped to, more than an address needs to know what should be shipped to it (see how that makes for awkward language as well?). From this it follows that an order belongs_to a billing address. The belongs_to side of the association is the one that stores the foreign key. Consider the case (in the near future), where you have repeat customers and you want to reuse their addresses. Then, the has_one :order in the address object turns into a has_many :orders. Cheers, Max> > 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 -~----------~----~----~----~------~----~------~--~---