Drowning in underscores and foreign keys

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

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 :smiley: Give it a shot.

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? :slight_smile:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs