ok, I''ve got a model called Location, the idea being that i have a bunch of locations aligned to a grid. Each location has a pointer to it''s neighbors, like so create_table "locations", :force => true do |t| t.string "name" t.text "description" t.integer "north" t.integer "northeast" t.integer "east" t.integer "southeast" t.integer "south" t.integer "southwest" t.integer "west" t.integer "northwest" t.integer "x" t.integer "y" t.datetime "created_at" t.datetime "updated_at" t.integer "map_id" end whereas all the directions (north, south, east, etc) should all be references to other locations. I know this is probably an easy question, but what kind of relationship is this, and how would I set it up in my model? like this? has_one :location, :foreign_key => :north has_one :location, :foreign_key => :northeast has_one :location, :foreign_key => :east has_one :location, :foreign_key => :southeast has_one :location, :foreign_key => :south has_one :location, :foreign_key => :southwest has_one :location, :foreign_key => :west has_one :location, :foreign_key => :northwest -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This is a self referential association class Location < ActiveRecordBase has_one :south, :class_name => ''Location'', :foreign_key => ''south'' end On Apr 28, 1:58 am, robo <chase.delanguille...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ok, I''ve got a model called Location, the idea being that i have a > bunch of locations aligned to a grid. Each location has a pointer to > it''s neighbors, like so > > create_table "locations", :force => true do |t| > t.string "name" > t.text "description" > t.integer "north" > t.integer "northeast" > t.integer "east" > t.integer "southeast" > t.integer "south" > t.integer "southwest" > t.integer "west" > t.integer "northwest" > t.integer "x" > t.integer "y" > t.datetime "created_at" > t.datetime "updated_at" > t.integer "map_id" > end > > whereas all the directions (north, south, east, etc) should all be > references to other locations. > > I know this is probably an easy question, but what kind of > relationship is this, and how would I set it up in my model? like > this? > > has_one :location, :foreign_key => :north > has_one :location, :foreign_key => :northeast > has_one :location, :foreign_key => :east > has_one :location, :foreign_key => :southeast > has_one :location, :foreign_key => :south > has_one :location, :foreign_key => :southwest > has_one :location, :foreign_key => :west > has_one :location, :foreign_key => :northwest > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
robo wrote:> ok, I''ve got a model called Location, the idea being that i have a > bunch of locations aligned to a grid. Each location has a pointer to > it''s neighbors, like so > > create_table "locations", :force => true do |t| > t.string "name" > t.text "description" > t.integer "north" > t.integer "northeast" > t.integer "east" > t.integer "southeast" > t.integer "south" > t.integer "southwest" > t.integer "west" > t.integer "northwest" > t.integer "x" > t.integer "y" > t.datetime "created_at" > t.datetime "updated_at" > t.integer "map_id" > end > > whereas all the directions (north, south, east, etc) should all be > references to other locations. > > I know this is probably an easy question, but what kind of > relationship is this, and how would I set it up in my model? like > this? > > has_one :location, :foreign_key => :north > has_one :location, :foreign_key => :northeast > has_one :location, :foreign_key => :east > has_one :location, :foreign_key => :southeast > has_one :location, :foreign_key => :south > has_one :location, :foreign_key => :southwest > has_one :location, :foreign_key => :west > has_one :location, :foreign_key => :northwestI think I would avoid this. If the grid is regular, then you should probably just store the X and Y coordinates in the model, then do something like class Location < AR::B def northeast @northeast ||= Location.find_by_x_and_y(x + 1, y + 1) end end Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Is there a reason why you would avoid setting up an association for each one? It seems like it''s basically the exact same thing you proposed, but a little more long-winded. No offense meant :) I mean what difference does it make if I create an association that finds its neighbors vs. writing a method that ends up finding the neighbors manually? On Apr 28, 8:20 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> robo wrote: > > ok, I''ve got a model called Location, the idea being that i have a > > bunch of locations aligned to a grid. Each location has a pointer to > > it''s neighbors, like so > > > create_table "locations", :force => true do |t| > > t.string "name" > > t.text "description" > > t.integer "north" > > t.integer "northeast" > > t.integer "east" > > t.integer "southeast" > > t.integer "south" > > t.integer "southwest" > > t.integer "west" > > t.integer "northwest" > > t.integer "x" > > t.integer "y" > > t.datetime "created_at" > > t.datetime "updated_at" > > t.integer "map_id" > > end > > > whereas all the directions (north, south, east, etc) should all be > > references to other locations. > > > I know this is probably an easy question, but what kind of > > relationship is this, and how would I set it up in my model? like > > this? > > > has_one :location, :foreign_key => :north > > has_one :location, :foreign_key => :northeast > > has_one :location, :foreign_key => :east > > has_one :location, :foreign_key => :southeast > > has_one :location, :foreign_key => :south > > has_one :location, :foreign_key => :southwest > > has_one :location, :foreign_key => :west > > has_one :location, :foreign_key => :northwest > > I think I would avoid this. If the grid is regular, then you should > probably just store the X and Y coordinates in the model, then do > something like > > class Location < AR::B > def northeast > @northeast ||= Location.find_by_x_and_y(x + 1, y + 1) > end > end > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.