# Has\_many fails where find\_by\_sql succeeds

**URL:** <https://www.ruby-forum.com/t/has-many-fails-where-find-by-sql-succeeds/93581>\
**Category:** Rails\
**Created:** [April 4, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/has-many-fails-where-find-by-sql-succeeds/93581 "2006-04-04T22:46:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [April 4, 2006, 10:46pm UTC](https://www.ruby-forum.com/t/has-many-fails-where-find-by-sql-succeeds/93581/1 "2006-04-04T22:46:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ashley\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Ashley\_M](https://www.ruby-forum.com/u/Ashley_M)\
**Post date:** [April 5, 2006, 7:18am UTC](https://www.ruby-forum.com/t/has-many-fails-where-find-by-sql-succeeds/93581/2 "2006-04-05T07:18:09Z")

</div>

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.
