Has_many :conditions option not working

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

In addition, this

Contact.find(session[:user]).target_lists

works - the :condition is recognized and executed.

Wes

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

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

check out

with_scope

in the api docs.
it is beautiful.

harp

…enjoy.
:slight_smile:

harp