- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有三个模型 - Patient
、Location
和 Area
。
每个患者都属于一个位置,而位置又属于区域。区域有一个名为 inpatient
的 bool 值。
我需要选择属于某个位置的患者,而该位置又属于住院值设置为 true 的区域。
我尝试了以下方法,但均无效:
@inpatient = Area.joins(:locations).joins(:patients).where(inpatient: true).count
@inpatient = Patient.joins(:location).join(:area).where(area: {inpatient: true})
@inpatient = Patient.joins(:area).where(area: {inpatient: true}).all
@inpatient = Patient.joins(:location).joins(:area).where(area: {inpatient: true}).count
非常感谢您的帮助!我想这很简单,我遇到了一些基本错误...我已经看过其他类似的问题,但无法将答案重新用于任何有效的问题。
模型
class Patient < ApplicationRecord
belongs_to :location, optional: true
has_one :area, through: :location
end
-
class Location < ApplicationRecord
has_many :patients
belongs_to :area, optional: false
end
-
class Area < ApplicationRecord
has_many :locations
has_many :patients, through: :locations
end
最佳答案
你很接近。您必须在 where 语句中使用表名,而不是关系名。此外,您必须在位置参数中使用区域。试试这个:
@inpatient = Patient.joins(:location).joins(:area).where(locations: {areas: {inpatient: true}})
关于ruby-on-rails - ActiveRecord .where of .join/has_one :through,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54506859/
试图为这个问题提出一个 has_one 关联。我有两个表: Ca: :id :serial ... Cert: :id :ca_id :serial ... 给定的 cas
我有以下 Person具有也是另一个 Person 的父级的类。我似乎无法弄清楚如何让关系发挥作用。 class Person 'Person', :primary_key => "mom_id",
对于 has_one关联我可以建立这样的关联: foo.build_bar() 我应该如何构建 has_one, through:协会? 例如: class Foo has_one :bar
这是我的模型: Class Audition belongs_to :video end Class Video has_one :audition end 和我的工厂: Factory.de
我有两个模型,用户和帐户。每个用户可以拥有一个帐户。 为用户创建帐户工作正常。我的问题是,当我尝试更新帐户时,以前的帐户 user_id 无效,并使用 user_id 创建了一个新的帐户行。我不希望这
我有 3 个相关的表/模型。我想检索组织派对的用户,给定一个派对记录,一个带有 bool 值的连接表 organized列和用户表。 迄今为止我最好的尝试(经过多次摆弄后,这对我来说最有意义)。我省略
我们有一个 Company、CompanyUser、User 和 Rating 模型,定义如下: 公司模式 class Company :job) 但这给了我错误,因为 @company.owner
我有一个使用 cocoon gem 的嵌套表单,深度为 5 个类。它们都是belongs_to和has_many,除了具有has_one和belongs_to关联的第二个最嵌套类之外。每当我编辑它时,
我有一个使用 cocoon gem 的嵌套表单,它包含 5 个类的深度。它们都是 belongs_to 和 has_many,除了一个是具有 has_one 和 belongs_to 关联的第二大嵌套
出席和凭单: belongs_to :event belongs_to :account 因此:出勤率和凭单之间是一对一的关系。 有没有办法不用我想太多就可以做到这一点? # attendment h
在组合 has_one 关联和 includes 时,我一直在尝试找出一些奇怪的行为。 class Post { order('comments.id DESC').limit(1) }, clas
Rails 的 has_one 在哪个级别运行? 例子: class User has_one :comment end class Comment belongs_to :user end
文件:User.rb has_one:自行车 文件:Bike.rb belongs_to:用户 这是我在 Bike 和 User 模型中的关系。我的数据库已经包含大量自行车和用户的数据。 如何使用不遵
我正在尝试从 has_one 关联中获取一些信息,但无法获得正确的调用方式。我有 2 个表,一个学生表,其中有一个 emergency_contact。 emergency_contact 表有一个外
我想要关系及其参数的基本语法 我需要这些说明 BELONGS_TO 和 HAS_ONE 的区别? 我想在 CGridView 上使用关系(该关系将另一个表元素称为外键)例如我有三张 table 信使
我刚刚发明了一个愚蠢的小辅助函数: def has_one(seq, predicate=bool): """Return whether there is exactly one item
我有两个模型 Project 和 ProjectPipeline。 我想创建一个项目表单,该表单也包含来自 ProjectPipeline 模型的字段。我已成功创建表单,但是当我点击保存时,值并未存储
我在drivers表中定义了has_one关系 has_one :current_haul 与hauls表关联。 还有 hauls 表和 drivers 表,它们都有 organization_id。
我有一个保存良好的主要对象。现在我在潜在客户和事件之间添加了 has_one 关联。一项事件有一个线索,其中线索具有外键 campaign_id。我的模型中有以下内容: class Campaign
在我的 Rails 应用程序中,我只要求用户在注册时输入电子邮件和姓名,然后让他们选择为他们的个人资料提供更完整的联系方式。因此,我有一个与 Contact.rb 关联的 User.rb 模型,即,
我是一名优秀的程序员,十分优秀!