ruby-on-rails - Rails 深度嵌套连接
问题描述
我有以下型号
class Doctor < ApplicationRecord
has_many :practices
has_many :facilities, through: :practices
has_one :address, as: :addressable
...
end
class Facility < ApplicationRecord
has_many :practices
has_many :doctors, through: :practices
has_one :address, as: :addressable
end
class Practice < ApplicationRecord
belongs_to :doctor
belongs_to :facility
end
class Address < ApplicationRecord
belongs_to :addressable, polymorphic: true
belongs_to :city
belongs_to :state
belongs_to :country
end
医生可以在一个cidy的不止一次设施中练习。我想使用连接(除非有更有效的方法)来查找在一个城市执业的所有医生的列表。我写了一个嵌套连接如下
Doctor.joins(facilities: [address: :city]) # Works
我无法解决编写用于指定城市名称的 where 子句的问题。
Doctor.joins(facilities: [address: [:city]]).where({facilities: {address: {city: {name: "Sydney"}}})
我收到一个错误,上面写着
Mysql2::Error: Unknown column 'address.name' in 'where clause'
请帮忙!
解决方案
你已经加载city
了address
,你在内存中有命名空间:
Doctor.joins(facilities: { address: :city }).where(cities: { name: city_name })
推荐阅读
- c++ - C++ standard compliance of MSVC versions (regarding std::set::erase)
- jenkins - 机器重启后无法访问 Jenkins
- c# - 正则表达式,提取变量名
- sap-cloud-sdk - 使用 S4Hana SDK 的补丁请求中的空值
- c - 检查读写系统调用的返回值的最佳方法是什么?
- robotframework - 我们如何使用 datadriver 库从 excel 中检索数据
- android - 如何在 android 中忽略 irelevent gps 位置更新?
- javascript - jquery如何比较两个数组
- azure-active-directory - 用户从 Azure AD 中删除后保持登录状态
- c++ - 具有局部 y 方向的向量与另一个向量的叉积