# Has\_many :conditions option not working

**URL:** <https://www.ruby-forum.com/t/has-many-conditions-option-not-working/66218>\
**Category:** Rails\
**Created:** [August 24, 2006, 1:44am UTC](https://www.ruby-forum.com/t/has-many-conditions-option-not-working/66218 "2006-08-24T01:44:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 24, 2006, 1:44am UTC](https://www.ruby-forum.com/t/has-many-conditions-option-not-working/66218/1 "2006-08-24T01:44:47Z")

</div>

All,

I have the following has\_many specification in a model

has\_many :target\_lists, :foreign\_key =\> :ContactNumber, :conditions =\>  
“Alias NOT LIKE ‘^%%’”

to filter out any rows with an Alias column that starts with ^

The condition is not being added to the SQL.

However, I have successfully used this exact same predicate in a call to  
paginate.

Any one ever seen this before?

Rails 1.1.6  
SQL Server (via ODBC)

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 24, 2006, 1:47am UTC](https://www.ruby-forum.com/t/has-many-conditions-option-not-working/66218/2 "2006-08-24T01:47:47Z")

</div>

In addition, this

Contact.find(session[:user]).target\_lists

works - the :condition is recognized and executed.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 24, 2006, 1:52am UTC](https://www.ruby-forum.com/t/has-many-conditions-option-not-working/66218/3 "2006-08-24T01:52:23Z")

</div>

OK I’m a dummy I see why this wasn’t working.

However, it brings up a DRY issue for me.

Is there any way to filter on a model such that no matter how you access  
the table you would always apply a condition?

Currently, in order to apply my condition I have to access the  
target\_list model through the has\_many relationship or code that same  
condition in any other finders.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 24, 2006, 9:00am UTC](https://www.ruby-forum.com/t/has-many-conditions-option-not-working/66218/4 "2006-08-24T09:00:17Z")

</div>

harper wrote:

> check out
> 
> with\_scope
> 
> in the api docs.  
> it is beautiful.
> 
> harp

That’s cool - I was hoping for some way to encapsulate what this does so  
that I can just reference that encapsulation everywhere.

But upon further thought, I could use “with\_scope” to override my  
finders, or maybe even better yet, just override the find method and  
always force my condition on there.

And even further thought - despite my hatred of views, I could simply  
throw a view in front of the table that handles my condition.

Thanks for introducing me to that method though - I am sure it will come  
in handy.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 24, 2006, 8:47am UTC](https://www.ruby-forum.com/t/has-many-conditions-option-not-working/66218/5 "2006-08-24T08:47:13Z")

</div>

check out

with\_scope

in the api docs.  
it is beautiful.

harp

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [August 24, 2006, 9:10am UTC](https://www.ruby-forum.com/t/has-many-conditions-option-not-working/66218/6 "2006-08-24T09:10:11Z")

</div>

…enjoy.  
🙂

harp
