开发者

Bug with Single Table Inheritance (STI) and has_and_belongs_to_many (HABTM) associations

Using Rails 3.0.7, and have 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

Calling:

Meeting.foo.to_sql

produces:

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 appears as if the final join relationship between "clients_administrators" and "users" has been missed. I think the last line of SQL should read:

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

Is this a bug? or have I misunderstood something?

Thanks


This appears to be a bug, but it has been resolved in Rails 3.1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜