Belongs_to <parent name>, :foreign_key modifier not working

All,

I have two ActiveRecord objects.

TargetList has_many Targets
Targets belongs_to TargetList

I’ve specified

belongs_to :target_list,
:foreign_key => ‘DataSetID’

Queries generated by various methods in target_list do not seem to see
the
foreign_key name and keep trying to query my Target table using
“target_list_id” (the default foreign key).

Anyone seen this before?

Wes

Wes G. wrote:

All,

I have two ActiveRecord objects.

TargetList has_many Targets
Targets belongs_to TargetList

I’ve specified

belongs_to :target_list,
:foreign_key => ‘DataSetID’

Queries generated by various methods in target_list do not seem to see
the
foreign_key name and keep trying to query my Target table using
“target_list_id” (the default foreign key).

Anyone seen this before?

Wes

To be more specific, in target_list.rb, I’m doing targets.size to get
the count of my child object, targets.

The query that’s generated correctly goes against the right table, but
the foreign_key specified is default.

I’m trying to puzzle through the ActiveRecord code on my own but it’s
pretty brutal. Here’s some more info.

I’ve discovered that by the time I get into the has_many_association.rb
count_records method, already the counter_sql is incorrect. In fact
when the has_many association is initialized, this SQL is incorrect.

I think that there may be an issue with the fact that both of my tables
(parent AND child) do a set_primary_key - I think that maybe
ActiveRecord can’t handle it.

And sadly, I don’t have any more time to figure out how this all works.

I’ll write a custom finder to do the <child_table>.size query. Sigh.

Wes

Oy vey!!!

OK, if it helps someone later, you have to specify a non-standard
foreign_key column between a parent and it’s child on BOTH sides of the
association.

So, in the child, do:

belongs_to :parent_object_name, :foreign_key => ‘X’

and in the parent, do:

has_many :child_object_name, :foreign_key => ‘X’

Wes

Wes G. wrote:

Oy vey!!!

OK, if it helps someone later, you have to specify a non-standard
foreign_key column between a parent and it’s child on BOTH sides of the
association.

So, in the child, do:

belongs_to :parent_object_name, :foreign_key => ‘X’

and in the parent, do:

has_many :child_object_name, :foreign_key => ‘X’

Oy vey, indeed!

I have a similar problem, with the complication that the tables in
question don’t belong to me, I’m just reporting off of them.

Table Products:
No real key/id field. Uniquified off of string “Product”

Table Versions:
No real key/id field.
Has foreign-key pointer to products, but field is called “program.”
Sure, whatever. :\

I’m having a heck of a time setting up my models’ belongs_to and
has_many lines. Can someone give me the syntax?

Thanks!