Error with unidirectional inheritance (STI) and has_and_belongs_to_many associations (HABTM)

Using Rails 3.0.7 and the following 4 models:

class User < ActiveRecord::Base
end

class Administrator < User
  has_and_belongs_to_many :clients
end

class Client < ActiveRecord::Base
  has_and_belongs_to_many :administrators
  has_and_belongs_to_many :meetings
end

class Meeting < ActiveRecord::Base
  has_and_belongs_to_many :clients

  def self.foo
    self.joins(:clients => :administrators)
  end
end

Vocation:

Meeting.foo.to_sql

gives:

SELECT meetings.*
FROM meetings
INNER JOIN clients_meetings ON clients_meetings.event_id = meetings.id
INNER JOIN clients ON clients.id = clients_meetings.child_id
INNER JOIN clients_administrators ON clients_administrators.child_id = clients.id
INNER JOIN users ON users.type = 'Administrator'

but it looks like the final connection connection between "clients_administrators" and "users" has been skipped. I think the last line of SQL should read:

INNER JOIN users ON users.id = clients_administrators.user_id
WHERE users.type = 'Administrator'

This is mistake? or didn’t I understand something?

thank

+3
source share
1 answer

This seems to be a bug but it is resolved in Rails 3.1.

+1
source

All Articles