# Limits of has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/limits-of-has-and-belongs-to-many/62744>\
**Category:** Rails\
**Created:** [January 19, 2006, 4:12am UTC](https://www.ruby-forum.com/t/limits-of-has-and-belongs-to-many/62744 "2006-01-19T04:12:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 19, 2006, 4:12am UTC](https://www.ruby-forum.com/t/limits-of-has-and-belongs-to-many/62744/1 "2006-01-19T04:12:02Z")

</div>

I’m experimenting with Rails on the Recipes example first before I dive  
into applying it to my intended application. I’m exploring which will be  
more work: to rename all the primary id’s in the legacy database, or to  
work around the fact that the primary id’s are not called ‘id’ within  
RoR instead.

I’ve been successful in using a join table with two fields named  
‘recipe\_id’ and ‘tag\_name’ to relate a table called Recipes with a table  
called Taggs, using this model:

class Recipe \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :taggs, :join\_table =\> “recipes\_tagnames” ,  
:association\_foreign\_key =\> “tag\_name”  
end

In this case both Recipes and Taggs have a field called ‘id’ as their  
primary key, i.e., they are being joined by Recipes.id =  
Recipes\_Tagnames.recipe\_id AND Recipes\_Tagnames.tag\_name = Taggs.id.

This works just fine, however for the legacy database in my intended  
application I prefer not to rename all of the primary id’s to ‘id’ just  
to get Rails’ slick conventions to work.

Apparently, according the current syntax, has\_and\_belongs\_to\_many cannot  
be made to work when either of the databases that are to be joined have  
their primary id named to something other than ‘id’. Is this correct?  
It appears that the options only provide additional flexibility within  
the join table.

Gracias!

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 19, 2006, 7:54am UTC](https://www.ruby-forum.com/t/limits-of-has-and-belongs-to-many/62744/2 "2006-01-19T07:54:22Z")

</div>

I wanna to hear the answer to this too. Our data models have been finely  
tuned and I doubt I’ll get clearance to tweak even field names to make  
my coding life easier - and if that’s what Ruby needs to rock & roll,  
then one of my colleagues has openly wondered if its ready for prime  
time. But I’m still giving it a go.

nat

dr plutes wrote:

> I’m experimenting with Rails on the Recipes example first before I dive  
> into applying it to my intended application. I’m exploring which will be  
> more work: to rename all the primary id’s in the legacy database, or to  
> work around the fact that the primary id’s are not called ‘id’ within  
> RoR instead.
> 
> I’ve been successful in using a join table with two fields named  
> ‘recipe\_id’ and ‘tag\_name’ to relate a table called Recipes with a table  
> called Taggs, using this model:
> 
> class Recipe \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :taggs, :join\_table =\> “recipes\_tagnames” ,  
> :association\_foreign\_key =\> “tag\_name”  
> end
> 
> In this case both Recipes and Taggs have a field called ‘id’ as their  
> primary key, i.e., they are being joined by Recipes.id =  
> Recipes\_Tagnames.recipe\_id AND Recipes\_Tagnames.tag\_name = Taggs.id.
> 
> This works just fine, however for the legacy database in my intended  
> application I prefer not to rename all of the primary id’s to ‘id’ just  
> to get Rails’ slick conventions to work.
> 
> Apparently, according the current syntax, has\_and\_belongs\_to\_many cannot  
> be made to work when either of the databases that are to be joined have  
> their primary id named to something other than ‘id’. Is this correct?  
> It appears that the options only provide additional flexibility within  
> the join table.
> 
> Gracias!

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 19, 2006, 10:45am UTC](https://www.ruby-forum.com/t/limits-of-has-and-belongs-to-many/62744/3 "2006-01-19T10:45:30Z")

</div>

nat wrote:

> I wanna to hear the answer to this too. Our data models have been finely  
> tuned and I doubt I’ll get clearance to tweak even field names to make  
> my coding life easier - and if that’s what Ruby needs to rock & roll,  
> then one of my colleagues has openly wondered if its ready for prime  
> time. But I’m still giving it a go.  
> Is the set\_primary\_key method not doing the do?

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 19, 2006, 3:57pm UTC](https://www.ruby-forum.com/t/limits-of-has-and-belongs-to-many/62744/4 "2006-01-19T15:57:01Z")

</div>

Alex Y. wrote:

> nat wrote:
> 
> > I wanna to hear the answer to this too.  
> > Is the set\_primary\_key method not doing the do?

How do the naming conventions work with a different primary key for  
things like has\_and\_belongs\_to\_many/has\_many/belongs\_to? Say in the  
recipes example above, if recipes.id were renamed to R\_ID and made  
primary w/ set\_primary\_key. Let’s play who wants to be a rails wonk:  
what foreign key would rails try to use in recipes\_tagnames instead of  
recipe\_id? (a) recipe\_R\_ID, (b) R\_ID, © recipe\_r\_id, or (d)  
recipe\_id? I’m guessing (d)… (do i get to move on to the next round  
regis?? 🙂

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 19, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/limits-of-has-and-belongs-to-many/62744/5 "2006-01-19T17:00:33Z")

</div>

A minor suggestion for the rails developers - might be nice to have  
something called set\_default\_foreign\_key. So if table People has  
primary key Pers\_ID and all other tables that link to it also use  
Pers\_ID to do so, if the People model is told “set\_default\_foreign\_key  
Pers\_ID” then thereafter for things like has\_many or habtm it would  
guess that the foreign key is Pers\_ID rather than people\_id. Hmmm…  
perhaps the model could also correctly guess the proper  
foreign\_association\_key as well for other models that have had a their  
own default foreign key specified by set\_default\_foreign\_key.

---

<div class="post-metadata">

**Author:** ![plutes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@plutes](https://www.ruby-forum.com/u/plutes)\
**Post date:** [January 19, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/limits-of-has-and-belongs-to-many/62744/6 "2006-01-19T16:38:49Z")

</div>

nat wrote:

> of recipe\_id? (a) recipe\_R\_ID, (b) R\_ID, © recipe\_r\_id, or (d)  
> recipe\_id? I’m guessing (d)… (do i get to move on to the next  
> round regis?? 🙂

Yup… According to the docs:

> :foreign\_key - specify the foreign key used for the association. By  
> default this is guessed to be the name of this class in lower-case  
> and “\_id” suffixed. So a Person class that makes a  
> has\_and\_belongs\_to\_many association will use “person\_id” as the  
> default foreign\_key.

–  
Alex
