【发布时间】:2020-09-23 19:33:26
【问题描述】:
我在相同的命名空间/模块中有两个模型:
module ReverseAuction
class Demand < ApplicationRecord
belongs_to :purchase_order, inverse_of: :demands, counter_cache: true
end
end
module ReverseAuction
class PurchaseOrder < ApplicationRecord
has_many :demands
end
end
请注意,我不必为模型指定 class_name,因为它们位于同一个模块中,并且这种关系运行良好。
当我尝试使用关系本身的名称查询 includes 时,它可以正常工作,例如:
ReverseAuction::PurchaseOrder.all.includes(:demands) # all right .. AR is able to figure out that *:demands* correspond to the 'reverse_auction_demands' table
但是当我尝试在这个查询中使用 where 时,AR 似乎无法自己找出(命名空间)表名,所以:
ReverseAuction::PurchaseOrder.includes(:demands).where(demands: {user_id: 1}) # gives me error: 'ERROR: missing FROM-clause entry for table "demands"'
但是如果我指定完整的解析(命名空间)模型名称,那么 where 会很顺利:
ReverseAuction::PurchaseOrder.includes(:demands).where(reverse_auction_demands: {user_id: 1}) # works pretty well
AR可以从includes中的关系推断命名空间模型的表名但不能在where,还是我没抓住重点?
【问题讨论】:
标签: ruby-on-rails ruby activerecord ruby-on-rails-6