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
Sharagoz
(Sharagoz)
April 28, 2010, 6:19am
2
This is a self referential association
class Location < ActiveRecordBase
has_one :south, :class_name => 'Location', :foreign_key => 'south'
end
11155
(-- --)
April 28, 2010, 1:20pm
3
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,
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?