大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 如何通过与Active Record关联找到has_many中缺少相关记录的记录?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
也就是说,主题通过关系有很多类别.
我认为很容易得到一个类别的主题
#Relationship Model Topic_id: Integer Category_id: Integer @topics=Topic.joins(:relationships)
但是,并非每个主题都有一个类别.那么我们如何检索没有类别的主题呢?
是否有减号查询?
也许它看起来像@ topics = Topic.where(‘id NOT IN(?)’,Relationship.all)
我在activerecord equivalent to SQL ‘minus’找到它但不确定这个解决方案.
@topics = Topic.joins('left join relationships on relationships.topic_id = topics.id').where('relationships.category_id is null')
或这个:
@topics = Topic .joins('left join relationships on relationships.topic_id = topics.id join categories on categories.id = relationships.category_id') .group('topics.id').having('count(categories.id) = 0')
以上是大佬教程为你收集整理的ruby-on-rails – 如何通过与Active Record关联找到has_many中缺少相关记录的记录?全部内容,希望文章能够帮你解决ruby-on-rails – 如何通过与Active Record关联找到has_many中缺少相关记录的记录?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。