This project is archived and is in readonly mode.
Models not loaded properly when using named_scope
-
joshsmoore (at gmail)
I just confirmed this still happens in edge rails.
-
Frederick Cheung
This is an odd interaction between named scopes and join based includes.
The :include means that your select looks like
SELECT ... FROM notes LEFT OUTER JOIN labels ...For the case you gave this would return two rows (because the note in question has 2 notes), however the first on the scope is limiting the select to just one row - so you only get one label back.
-
Frederick Cheung
- State changed from new to invalid
D'oh, read too quickly. The start of what I said was correct, the problem you get is that the conditions (labels.label = '...') applies to the whole query.
The named scope here is irrelevant,
Note.find :all, :include => :labels, :conditions =>['labels.label =?', 'test']will exhibit the same problem: all of the notes loaded in this way will have exactly one label, because the conditions prevent any other labels from being loaded by the big join query.
What this boils down to is that :include with :conditions on the included table has dubious semantics. If you don't actually care about the eager loading,
named_scope :has_labels, lambda { |label| {:joins => :labels, :conditions => ['labels.label =?', label]}}would select the correct notes.