Rails:HABTM - 查找没有关联的所有记录

时间:2012-07-01 03:23:13

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 has-and-belongs-to-many

我有2个模型(Workout,Equipment)中有一个属于很多关系。如果我使用Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = 5")它可以工作,但是如果我使用Workout.find(:all, :joins => :equipment, :conditions => "equipment.id = null")它不会返回没有关联的记录。有什么想法吗?

1 个答案:

答案 0 :(得分:10)

给它一个旋转;

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