Has_many :through fails on n-way join

I think this is a Rails bug, but I’d like to describe it here first -
in case I’m doing something wrong.

When using an n-way (has_many :through) join, the wrong model attr is
selected when evaluating the join relationship.

Consider the following models. User, Group, Customer represent a
standard HMT relationship. User, Group, Build (UGB) is an n-way style
join - where the relationship between Users and Builds is possible
since Builds belong to Customers who are joined to Users. Note the
additional attributes to the HMT in the Build model (ie, :foreign_key
& :primary_key).

class User < ActiveRecord::Base
has_many :groups
has_many :customers, :through => :groups

class Group < ActiveRecord::Base
belongs_to :user
belongs_to :customer

class Customer < ActiveRecord::Base
has_many :builds

has_many :groups
has_many :members, :through => :groups, :source => :user

class Build < ActiveRecord::Base
belongs_to :customer

has_many :groups, :foreign_key => :customer_id, :primary_key
=> :customer_id
has_many :accessors, :through => :groups, :source => :user

The problem is that the @build.accessors results in an empty [] -
because the SQL is using the @build.id rather than
@build.customer_id. See related SQL listings…
User Load (2.6ms) SELECT * FROM “users” WHERE (“users”.“login” =
E’hp_person’) LIMIT 1
Customer Load (2.5ms) SELECT “customers”.* FROM “customers” INNER
JOIN groups ON customers.id = groups.customer_id WHERE
((“groups”.user_id = 1046015453)) LIMIT 1
Build Load (2.4ms) SELECT * FROM “builds” WHERE
(“builds”.customer_id = 807266524)
User Exists (11.5ms) SELECT “users”.id FROM “users” INNER JOIN
groups ON users.id = groups.user_id WHERE (“users”.“id” = 1046015453)
AND ((“groups”.customer_id = 192574979)) LIMIT 1

builds table:
:id => 192574979;
:customer_id => 807266524;

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