Has_many fails where find_by_sql succeeds

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’

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

Ashley M. 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.