Caleb Fidecaro
Caleb Fidecaro

Reputation: 141

Nearby localities in Rails model

I have a model called Locality, which is basically a city (or a suburb within a big city).

Each Locality has a short list of the Localities which are adjacent - this is to help in searching.

I have read dozens of posts on here regarding self-referential has_many, :through associations and so on, but I'm still having trouble figuring this out.

Essentially what I'd like to end up with is:

l = Locality.find(1)
n = l.nearby_localities
# n should now hold a list of Locality ids

What's the best way for this to be done?

Upvotes: 3

Views: 117

Answers (1)

Benjamin Tan Wei Hao
Benjamin Tan Wei Hao

Reputation: 9691

How do you determine nearby_localities?

Actually, you could just have it a simple method.

class Localities < ActiveRecord::Base

  # Previous Code ...

  def nearby_localities
    # Implementation here.
  end

end

Keep it simple!

Upvotes: 1

Related Questions