Nick5a1
Nick5a1

Reputation: 917

Rails: HABTM - find all records with no association

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?

Upvotes: 9

Views: 4197

Answers (1)

James
James

Reputation: 2293

Give this a whirl;

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

Upvotes: 11

Related Questions