如何查询基于另一个模型的属性的模型属于第一种模式? [英] How to query a model based on attribute of another model which belongs to the first model?
本文介绍了如何查询基于另一个模型的属性的模型属于第一种模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个模型人
,其中的has_many
车
每个车
可以是类型车
或摩托车
,我怎么可以查询所有的人,谁拥有汽车和所有的人,谁拥有摩托车?
我不认为这是正确的:
Person.joins(:辆)。凡(汽车类型:自动)
Person.joins(:辆)。凡(汽车类型:'摩托车')
解决方案
您可以执行如下:
Person.includes(:辆)。凡(车辆:{类型:'自动'})
Person.includes(:辆)。凡(车辆:{类型:'摩托车'})
要当心与 .joins
和 .includes
:
#考虑这些模型
帖子
belongs_to的:用户
#^ ^
用户
的has_many:帖子
#^
#在`包括/ joins`方法使用在模型中定义的名称:
User.includes(:帖子)。凡(帖子:{标题:博比表'})
#^ ^
#但是`where`采用多元化的版本(表的名称):
Post.includes(:用户)。凡(网友:{名称:博比})
#^ ^ ^ ^
有一个很微妙的:
发布
belongs_to的:作者,将class_name:用户
用户
的has_many:帖子
Post.includes(:作者)。凡(网友:{名称:'约翰'})
类似的问题:
- <一个href="http://stackoverflow.com/questions/19231629/association-named-not-found-perhaps-misspelled-issue-in-rails-association/19231664#19231664">association命名未发现可能拼错的问题,在轨协会
- <一个href="http://stackoverflow.com/questions/18234602/rails-active-record-querying-association-with-exists/18234998#18234998">Rails与活动记录查询协会'存在'
- <一个href="http://stackoverflow.com/questions/13515225/rails-3-has-one-has-many-with-lambda-condition/13516285#13516285">Rails 3,HAS_ONE /的has_many与拉姆达条件
- <一个href="http://stackoverflow.com/questions/18082096/rails-4-scope-to-find-parents-with-no-children/18082147#18082147">Rails 4范围找父母没有子女
- <一个href="http://stackoverflow.com/questions/24266069/join-multiple-tables-with-active-records/24266583#24266583">Join与活跃记录多个表
- <一个href="http://stackoverflow.com/questions/25210569/rails-finding-all-users-whose-relationship-has-a-specified-attribute/25210625#25210569">Rails:查找所有用户的关系已经指定属性
If I have a model Person
, which has_many
Vehicles
and each Vehicle
can be of type car
or motorcycle
, how can I query for all persons, who have cars and all persons, who have motorcycles?
I don't think these are correct:
Person.joins(:vehicles).where(vehicle_type: 'auto')
Person.joins(:vehicles).where(vehicle_type: 'motorcycle')
解决方案
You can do as following:
Person.includes(:vehicles).where(vehicles: { type: 'auto' })
Person.includes(:vehicles).where(vehicles: { type: 'motorcycle' })
Be carefull with .joins
and .includes
:
# consider these models
Post
belongs_to :user
#^^
User
has_many :posts
#^
# the `includes/joins` methods use the name defined in the model :
User.includes(:posts).where(posts: { title: 'Bobby Table' })
#^ ^
# but the `where` uses the pluralized version (table's name) :
Post.includes(:user).where(users: { name: 'Bobby' })
#^^^ ^
A tricky one:
Post
belongs_to :author, class_name: 'User'
User
has_many :posts
Post.includes(:author).where(users: { name: 'John' })
Similar questions:
- association named not found perhaps misspelled issue in rails association
- Rails active record querying association with 'exists'
- Rails 3, has_one / has_many with lambda condition
- Rails 4 scope to find parents with no children
- Join multiple tables with active records
- Rails: Finding all Users whose relationship has a specified attribute
这篇关于如何查询基于另一个模型的属性的模型属于第一种模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文