I''m working with a has_many :through association, where a Member has_many Projects through ProjectViewers. The problem I''m running into is that Rails doesn''t load the association so line (3) fails until I explicitly access a member of the collection (in this case, I iterated it). Is there a better way? Thanks, Steve 1. assigns[:member].should_not_be_nil 2. assigns[:member].projects.each{|p| puts "project #{p.name}"} # force association to be loaded 3. assigns[:member].projects.size.should_be 1
Hi, Instead of assigns[:member].projects.size.should_be 1 use assigns [:member].projects.count.should_be 1 AR associations is returned as enumerable (Array) where Enumerable#size wouldn''t force a DB query with the recent association loading patches on Edge ( likely 1.2 as well ). association.count forces roundtrip to the DB, effectively dealing with your line #2 in the process. Just a thought, works fine for me with #count, been using Rspec since 0.7.2 on Edge and most of these little quirks/errors occur at a higher level than Rspec itself. - Lourens On 2006/12/13, at 00:48, s.ross wrote:> I''m working with a has_many :through association, where a Member > has_many Projects through ProjectViewers. The problem I''m running > into is that Rails doesn''t load the association so line (3) fails > until I explicitly access a member of the collection (in this case, I > iterated it). Is there a better way? > > Thanks, > > Steve > > 1. assigns[:member].should_not_be_nil > 2. assigns[:member].projects.each{|p| puts "project #{p.name}"} # > force association to be loaded > 3. assigns[:member].projects.size.should_be 1 > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Right you are. Using count works perfectly. Steve On Dec 12, 2006, at 6:54 PM, Lourens Naude wrote:> Hi, > > Instead of assigns[:member].projects.size.should_be 1 use assigns > [:member].projects.count.should_be 1 > AR associations is returned as enumerable (Array) where > Enumerable#size wouldn''t force a DB query with the recent association > loading patches on Edge ( likely 1.2 as well ). > > association.count forces roundtrip to the DB, effectively dealing > with your line #2 in the process. > > Just a thought, works fine for me with #count, been using Rspec since > 0.7.2 on Edge and most of these little quirks/errors occur at a > higher level than Rspec itself. > > - Lourens > > On 2006/12/13, at 00:48, s.ross wrote: > >> I''m working with a has_many :through association, where a Member >> has_many Projects through ProjectViewers. The problem I''m running >> into is that Rails doesn''t load the association so line (3) fails >> until I explicitly access a member of the collection (in this case, I >> iterated it). Is there a better way? >> >> Thanks, >> >> Steve >> >> 1. assigns[:member].should_not_be_nil >> 2. assigns[:member].projects.each{|p| puts "project #{p.name}"} # >> force association to be loaded >> 3. assigns[:member].projects.size.should_be 1 >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On 12/13/06, s.ross <cwdinfo at gmail.com> wrote:> Right you are. Using count works perfectly. > > Steve > > On Dec 12, 2006, at 6:54 PM, Lourens Naude wrote: > > > Hi, > > > > Instead of assigns[:member].projects.size.should_be 1 use assigns > > [:member].projects.count.should_be 1 > > AR associations is returned as enumerable (Array) where > > Enumerable#size wouldn''t force a DB query with the recent association > > loading patches on Edge ( likely 1.2 as well ).I believe that this will work as well: assigns[:member].should_have(1).projects This uses #length under the hood when available, which seems to have the same effect as #count.> > > > association.count forces roundtrip to the DB, effectively dealing > > with your line #2 in the process. > > > > Just a thought, works fine for me with #count, been using Rspec since > > 0.7.2 on Edge and most of these little quirks/errors occur at a > > higher level than Rspec itself. > > > > - Lourens > > > > On 2006/12/13, at 00:48, s.ross wrote: > > > >> I''m working with a has_many :through association, where a Member > >> has_many Projects through ProjectViewers. The problem I''m running > >> into is that Rails doesn''t load the association so line (3) fails > >> until I explicitly access a member of the collection (in this case, I > >> iterated it). Is there a better way? > >> > >> Thanks, > >> > >> Steve > >> > >> 1. assigns[:member].should_not_be_nil > >> 2. assigns[:member].projects.each{|p| puts "project #{p.name}"} # > >> force association to be loaded > >> 3. assigns[:member].projects.size.should_be 1 > >> > >> > >> _______________________________________________ > >> rspec-users mailing list > >> rspec-users at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >