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