大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 如何根据属于第一个模型的另一个模型的属性查询模型?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我不认为这些是正确的:
Person.joins(:vehicles).where(vehicle_type: 'auto') Person.joins(:vehicles).where(vehicle_type: 'motorcycle')
Person.includes(:vehicles).where(vehicles: { type: 'auto' }) Person.includes(:vehicles).where(vehicles: { type: 'motorcycle' })
小心.joins和.includes:
# consider these models Post # table name is posts belongs_to :user #^^ User # table name is users has_many :posts #^ # the `includes/joins` methods use the relation name defined in the model: User.includes(:posts).where(posts: { title: 'Bobby Table' }) #^ ^ # but the `where` uses the exact table name: Post.includes(:user).where(users: { name: 'Bobby' }) #^^^ ^
一个棘手的问题:
Post belongs_to :author,class_name: 'User' User # table named users has_many :posts Post.includes(:author).where(users: { name: 'John' }) # because table is named users
类似的问题:
> 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
以上是大佬教程为你收集整理的ruby-on-rails – 如何根据属于第一个模型的另一个模型的属性查询模型?全部内容,希望文章能够帮你解决ruby-on-rails – 如何根据属于第一个模型的另一个模型的属性查询模型?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。