# Drowning in underscores and foreign keys

**URL:** <https://www.ruby-forum.com/t/drowning-in-underscores-and-foreign-keys/72767>\
**Category:** Rails\
**Created:** [October 18, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/drowning-in-underscores-and-foreign-keys/72767 "2006-10-18T19:13:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [October 18, 2006, 7:13pm UTC](https://www.ruby-forum.com/t/drowning-in-underscores-and-foreign-keys/72767/1 "2006-10-18T19:13:10Z")

</div>

Hi everybody,

I’m working with a legacy schema where they saw fit to underscore  
everything in sight, and I’m wondering if that’s screwing up my  
associations.

I have a table COMMUNITY\_TABLE that has a COMMUNITY\_ID as well as a  
COMMUNITY\_TYPE\_ID. COMMUNITY\_TYPE\_ID is a pointer to the  
COMMUNITY\_TYPE\_TABLE, which has a COMMUNITY\_TYPE\_ID as its primary key,  
as well as a string COMMUNITY\_TYPE\_TABLE. I am translating this as  
“CommmunityType has\_many Communities, and Community belongs\_to  
CommmunityType”:

class CommunityType \< ActiveRecord::Base  
set\_table\_name “COMMUNITY\_TYPE\_TABLE”  
set\_primary\_key “COMMUNITY\_TYPE\_ID”

has\_many :communities  
end

class Community \< ActiveRecord::Base  
set\_table\_name “COMMUNITY\_TABLE”  
set\_primary\_key “COMMUNITY\_ID”

belongs\_to :community\_type  
end

What I’d like to do is is start at a community type where my title is  
“Private” (for example), and then get all the communities that are of  
that type:

CommunityType.find\_by\_community\_type\_title(“Private”).communities

But this always returns []. I know I must be missing something but I  
can’t figure out what. I know that I have data, because I can do this:

Community.find(:first, :conditions=\>[“community\_type\_id=?”,  
CommunityType.find\_by\_community\_type\_title(“Private”).community\_type\_id])

and have it return a value. Similarly I can do this:

Community.find\_first.community\_type.community\_type\_title

And have it give back the right answer. So Community knows that it has  
a pointer into CommunityType, but not the other way around.

Anybody know what probably obvious thing I’ve missed?

Thanks,

D

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [October 18, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/drowning-in-underscores-and-foreign-keys/72767/2 "2006-10-18T20:54:52Z")

</div>

First off: Did you have a typo when saying:  
“s a pointer to the  
COMMUNITY\_TYPE\_TABLE, which has a COMMUNITY\_TYPE\_ID as its primary key,  
as well as a string COMMUNITY\_TYPE\_TABLE.”  
did you mean "as a string “COMMUNITY\_TYPE\_TITLE” ? cos i never saw that  
title you refer to later anywhere in that explenation

Second: I’m pretty new to this stuff myself, so i just throw in my 2  
cents, no promise it will work:

Tird: try this:

class CommunityType \< ActiveRecord::Base  
set\_table\_name “COMMUNITY\_TYPE\_TABLE”  
set\_primary\_key “COMMUNITY\_TYPE\_ID”

has\_many :communities, :foreign\_key =\> “community\_type\_id”

end

class Community \< ActiveRecord::Base  
set\_table\_name “COMMUNITY\_TABLE”  
set\_primary\_key “COMMUNITY\_ID”

belongs\_to :community\_type, :foreign\_key =\> “community\_type\_id”  
end

i’m not sure if you have to specify :foregn\_key in the belongs\_to, but  
for sure in the has\_many, cos otherwise Rails would assume that the  
foreign\_key in the COMMUNITY\_TABLE would be the primary key  
“community\_id”, but its “community\_table\_id”

… or i got you wrong or all twisted up 😃 Give it a shot.

---

<div class="post-metadata">

**Author:** ![Duane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b4bc9f/32.png) [@Duane](https://www.ruby-forum.com/u/Duane)\
**Post date:** [October 18, 2006, 10:28pm UTC](https://www.ruby-forum.com/t/drowning-in-underscores-and-foreign-keys/72767/3 "2006-10-18T22:28:56Z")

</div>

> as well as a string COMMUNITY\_TYPE\_TABLE."  
> did you mean "as a string “COMMUNITY\_TYPE\_TITLE” ? cos i never saw that  
> title you refer to later anywhere in that explenation

Yes, should have said COMMUNITY\_TYPE\_TITLE. See what I mean about  
drowning in it? 🙂

> has\_many :communities, :foreign\_key =\> “community\_type\_id”  
> belongs\_to :community\_type, :foreign\_key =\> “community\_type\_id”

> i’m not sure if you have to specify :foregn\_key in the belongs\_to, but  
> for sure in the has\_many, cos otherwise Rails would assume that the  
> foreign\_key in the COMMUNITY\_TABLE would be the primary key  
> “community\_id”, but its “community\_table\_id”

Didn’t fix it. I’m not sure you’re accurate there, though – if I’ve  
said that table B has\_many A’s, then I believe the expected behavior is  
to find B\_id inside the A table, not A\_id. A\_id would be the primary  
key for A, so B couldn’t use that to find A’s that belonged to B.

Thanks for the ideas,  
D
