Rails 5: Table 名称未通过 class_name 推断

Rails 5: Table name not being inferred through class_name

我有以下 rails 型号:

class Connection < ApplicationRecord
  belongs_to :graph
  has_one :node, class_name: 'Person'
end

每个connection.node指向一个Person

我希望能够找到指向特定人的联系。

例如:

Connection.joins(:nodes).where(node: {first_name: 'Jane'} )

但是这给了我错误:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "node"
LINE 1: SELECT "connections".* FROM "connections" WHERE "node"."firs...
                                                        ^
: SELECT "connections".* FROM "connections" WHERE "node"."first_name" = 

我相信这是因为它查询 nodes 而不是它需要的 people

我也试过 joins(:people),但出现错误,没有找到名为 people 的关联。

有什么想法可以实现这一点,而不必重命名关联?

joins/includes中,您将协会名称(单数(如果has_one)或复数(如果has_many))放入where您输入 table 名称的子句:

Connection.joins(:node).where(people: {first_name: 'Jane'} )