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 <[email protected]> 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
> [email protected]
> --
> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> 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 [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