Rails: HABTM - find all records with no association

心不动则不痛 提交于 2019-12-01 15:02:09

问题


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?


回答1:


Give this a whirl;

Workout.joins("left join equipments e on workouts.id = e.workouts_id").where("e.id is null")


来源:https://stackoverflow.com/questions/11279317/rails-habtm-find-all-records-with-no-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!