本文介绍了Rails:HABTM-查找没有关联的所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个模型(锻炼,设备),并且属于许多关系.如果使用Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = 5")
,它可以工作,但是如果使用Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = null")
,它不会返回没有关联的记录.有什么想法吗?
I have 2 models (Workout, Equipment) in a has and belongs to many relationship. If I use Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = 5")
it works, but if I use Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = null")
it doesn't return the records with no association. Any ideas?
推荐答案
请稍等一下.
Workout.joins("left join equipments e on workouts.id = e.workouts_id").where("e.id is null")
这篇关于Rails:HABTM-查找没有关联的所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!