gpt4 book ai didi

ruby-on-rails - 单表继承 (STI) 和 has_and_belongs_to_many (HABTM) 关联的错误

转载 作者:行者123 更新时间:2023-12-04 07:36:46 25 4
gpt4 key购买 nike

使用 Rails 3.0.7,并有以下 4 个模型:

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

来电:
Meeting.foo.to_sql

产生:
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'

但似乎错过了“clients_administrators”和“users”之间的最终加入关系。我认为 SQL 的最后一行应该是:
INNER JOIN users ON users.id = clients_administrators.user_id
WHERE users.type = 'Administrator'

这是一个错误吗?还是我误解了什么?

谢谢

最佳答案

这似乎是 bug ,但它已在 Rails 3.1 中解决。

关于ruby-on-rails - 单表继承 (STI) 和 has_and_belongs_to_many (HABTM) 关联的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com