belongs_to association using composite key with "id" as a member fails

8 views
Skip to first unread message

Tom Hughes

unread,
Nov 9, 2008, 11:50:55 AM11/9/08
to Composite Keys for ActiveRecord
I have a model which uses a composite primary key, one of whose
members is a column named "id" which is declared like this:

set_primary_keys :id, :sequence_id

That model is then declared to belong to a second model:

belongs_to :way, :foreign_key => :id

Loading the first model, and using :include to pull in the second one,
fails due to too many placeholders appearing in the SQL generated.

The reason is that composite_primary_keys is using record.send(k) to
obtain the values of the key components, but "id" is of course a
method that returns an array of all the key components rather than
fetching the actual field value. Changing the code to use
record.attributes[k] instead seems to fix the problem:

http://groups.google.com/group/compositekeys/web/cpk-belongs-to-id.patch

There are other places in the code which look like they are fetching
the key components the same way, so this might be a more widespread
problem?

Tom
Reply all
Reply to author
Forward
0 new messages