# Has\_and\_belongs\_to\_many :neigborcities

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-neigborcities/112093>\
**Category:** Rails\
**Created:** [August 15, 2007, 10:08am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-neigborcities/112093 "2007-08-15T10:08:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b5e925/32.png) [@ina](https://www.ruby-forum.com/u/ina)\
**Post date:** [August 15, 2007, 10:08am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-neigborcities/112093/1 "2007-08-15T10:08:51Z")

</div>

Hi, I am new to Ruby on Rails and - of course - have a problem…

I have cities and I want to define neighborcities. I want to do this  
via a second table (cities\_cities), where the city\_ids are going to be  
connected. Now, if city 1 is a neighbor of city 2, then city 2 is a  
neighbor of city 1, so to keep it consistent, I want to save it in the  
database with the smaller city\_id as city\_id1, the higher id as  
city\_id2.  
Then my city Model would get the definition

```
has_and_belongs_to_many :neighborcities
     :class_name => "City"
```

## ``` cities ```

id  
name

## cities\_cities

city\_id1  
city\_id2

But then I am stuck, because with this definition

```
has_and_belongs_to_many :neighborcities
     :class_name => "City",
     :foreign_key => "city_id1",
     :association_foreign_key => "city_id2"
```

I only get the cities where city\_id1 is defined in the database, not  
the other way round.

Then I thougt about putting a finder\_sql

```
:finder_sql => "SELECT cities.* FROM cities
     LEFT OUTER JOIN cities_cities cc1 ON cities.id = cc1.cities_id2
     LEFT OUTER JOIN cities_cities cc2 ON cities.id = cc2.cities_id1
     WHERE (cc1.cities_id1 = #{id} OR cc2.cities_id2 = #{id} )"
```

But the #{id} does not return the expected id of the city I am  
requesting, but some high number (546214050).

Now: am I going the best way for my project at all? What would be the  
best way to connect 2 cities with each other (same problem would be  
products has\_and\_belongs\_to\_many :similarprodudcts)? There has to be a  
simpler solution.  
And if I have to use the finder\_sql: how would the update\_sql look  
like?

Any help appreciated,  
thanks, greetings from Berlin,

Ina

---

<div class="post-metadata">

**Author:** ![ina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b5e925/32.png) [@ina](https://www.ruby-forum.com/u/ina)\
**Post date:** [August 15, 2007, 1:05pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-neigborcities/112093/2 "2007-08-15T13:05:14Z")

</div>

On Aug 15, 1:07 am, ina [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## has\_and\_belongs\_to\_many :neighborcities
> 
> But the #{id} does not return the expected id of the city I am  
> requesting, but some high number (546214050).
> 
> Now: am I going the best way for my project at all? What would be the  
> best way to connect 2 cities with each other (same problem would be  
> products has\_and\_belongs\_to\_many :similarprodudcts)? There has to be a  
> simpler solution.  
> And if I have to use the finder\_sql: how would the update\_sql look  
> like?

This sounds like what people have coded for networks of friends, i.e.  
self-referential many to many, without the complication of inviting  
people and having people be pending friends

[http://railsforum.com/viewtopic.php?pid=8007](http://railsforum.com/viewtopic.php?pid=8007)  
[http://svn.dnite.org/has\_many\_friends/README](http://svn.dnite.org/has_many_friends/README)
