ruby-on-rails - 范围内的 Rails 关联
问题描述
我有两个不直接相互关联的模型,Client
我PotentialClient
希望能够join
在它们之间创建一个。
两种模型都有该字段mindbody_id
,但它仅在Location
. 两者都Client
属于PotentialClient
一个Location
。
class Client < ApplicationRecord
belongs_to :location
end
class PotentialClient < ApplicationRecord
belongs_to :location
end
class Location < ApplicationRecord
has_many :potential_clients
has_many :clients
end
鉴于它既存在于 a 中又存在于 a 中,我如何Client
加入PotentialClient
mindbody_id Location
?
我认为这样的事情会起作用,但它没有:
#client.rb
has_one :potential_client, -> {
where(potential_clients: {location_id: self.location_id, mindbody_id: self.mindbody_id})
}
解决方案
我认为这应该有效
has_one :potential_client, -> { where(mindbody_id: mindbody_id }, through: :location
这应该从该位置获取所有“potential_clients”,然后应用范围,您应该只有一个 PotentialClient
推荐阅读
- c# - 如何在 Hololens 应用中调用默认浏览器
- python - seaborn 分布图为每个直方图 bin 的计数添加标签
- javascript - WithStore:无法读取未定义的属性“状态”(纯反应轮播)
- optimization - 如何使用 MathProg (GLPK) 或其 API 实现极小极大组合优化问题
- ruby-on-rails - rails 迁移更改了不相关的列
- c++ - std::move 和 C++ 中的引用传递有什么区别?
- vba - 在 QueryDefs 中找不到 RecordsetType 属性作为有效属性
- html - 如何在 CSS 中设置默认页面尺寸?
- php - 连接到 AWS RDS 数据库的非 AWS 服务器要慢得多
- swift - UIButton 未添加到子视图 swift