Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 708
Nick5a1 Avatar asked Jul 01 '12 03:07

Nick5a1


1 Answers

Give this a whirl;

Workout.joins("left join equipments e on workouts.id = e.workouts_id").where("e.id is null")
like image 90
Jamsi Avatar answered Sep 27 '22 22:09

Jamsi