我正在尝试执行一个 where 语句,该语句仅显示具有特定关联的结果。
例如:
一个公司通过公司状态有多种状态。他们可以有多种状态,可以是金、银和/或青铜,或者根本没有。我试图让我的结果只返回具有状态(金、银和/或铜)的公司,而不返回没有状态的公司。
I am trying do a where statement that only brings up results that have that particular association.
For example:
A company has many statuses through company statuses. They can have multiple statuses that can be gold, silver, and/or bronze, or none at all. I am trying to have my results only return the companies that have a status (gold, silver, and/or bronze) and not the ones have no statuses.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的用例没有很好地阐明,但我认为您想要的是找到具有与指定状态匹配的状态的公司:
假设有一家公司,这应该会给您正确的查询
has_many:状态
。Your use case isn't very well articulated, but I think what you're wanting is to find Companies that have a status that matches a specified one:
That should give you the right query, assuming a Company
has_many :statuses
.来自 Ruby on Rails 关于 Active Record Associations 的指南:
http://guides.rubyonrails .org/association_basics.html#detailed-association-reference
From the Ruby on Rails guide on Active Record Associations:
http://guides.rubyonrails.org/association_basics.html#detailed-association-reference
如果您有一个名为 Companies_statuses 的关联表:
检索具有至少一种状态的所有公司
检索没有状态的所有公司
If you have an associative table called companies_statuses:
to retrieve all companies with at least one status
to retrieve all companies with no status