Forum: Ruby on Rails has_many fails where find_by_sql succeeds

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-04 22:46
(Received via mailing list)
Can anyone explain why the method form of the following works, but
the (apparently identical) has_many fails?

I have 3 sites in my fixtures.  Initially, for a new StockGroup,
sites_not_using_this_stock_group should return all 3 sites (which it
does).  When you add a site to the StockGroup, it should disappear
from the output of sites_not_using_this_stock_group.  But the
has_many form returns all 3 sites.

I can't see any difference myself?

Thanks
Ashley




class StockGroup < ActiveRecord::Base
   has_and_belongs_to_many :sites
   has_many :sites_not_using_this_stock_group,
             :class_name => "Site",
             :finder_sql => 'select sites.* from sites
                             where id not in
                                (select site_id from sites_stock_groups
                                 where stock_group_id = #{id})
                             order by id asc'

   <snip>

   def sites_not_using_this_stock_group
     Site.find_by_sql(["select sites.* from sites
                         where id not in
                           (select site_id from sites_stock_groups
                            where stock_group_id = #{id})
                         order by id asc"])
   end

end

class StockGroupTest < Test::Unit::TestCase
   fixtures :stock_groups, :sites

   def test_sites_not_using_this_stock_group
     my_first_site = sites(:my_first_site)

     # assert that all sites are not in an empty group
     stock_group = StockGroup.create(:description => "Dummy Stock
Group", :dataset_id => 1, :image_source_id => 2)
     sites = stock_group.sites_not_using_this_stock_group
     all_sites = Site.find(:all, :order => "id asc")
     assert_equal all_sites, sites

     # assert that a site added to the group does not appear in the
sites not in this group
     first_site = Site.find(1)
     stock_group.sites << first_site
     assert stock_group.save
     sites = stock_group.sites_not_using_this_stock_group
     all_other_sites = Site.find(:all, :conditions => "id <> #
{first_site.id}", :order => "id asc")
     assert_equal all_other_sites, sites
   end
end
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-04-05 07:18
(Received via mailing list)
Ashley Moran wrote:
> Can anyone explain why the method form of the following works, but  the
> (apparently identical) has_many fails?

has_many queries are cached by default.  To force a reload, write:

         sites = stock_group.sites_not_using_this_stock_group(true)

--
We develop, watch us RoR, in numbers too big to ignore.
This topic is locked and can not be replied to.