# Association :conditions & instance-variable evaluation

**URL:** <https://www.ruby-forum.com/t/association-conditions-instance-variable-evaluation/120718>\
**Category:** Rails\
**Created:** [November 20, 2007, 1:35am UTC](https://www.ruby-forum.com/t/association-conditions-instance-variable-evaluation/120718 "2007-11-20T01:35:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [November 20, 2007, 1:35am UTC](https://www.ruby-forum.com/t/association-conditions-instance-variable-evaluation/120718/1 "2007-11-20T01:35:03Z")

</div>

Hello,

I was wondering if it’s possible to add :conditions to an association,  
such that the conditions reference variables that need to be re-  
evaluated before each query?

e.g.:  
has\_many :people, :conditions =\> “some\_column =  
@some\_instance\_variable, revision\_time \> #{Time.now},” etc.

When I tried an example binding a date column to Time.now, the time  
didn’t change with each query. Instead, it continued using the  
initial value of Time.now from when the class loaded.

Thanks,  
Tom

---

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [November 20, 2007, 5:07am UTC](https://www.ruby-forum.com/t/association-conditions-instance-variable-evaluation/120718/2 "2007-11-20T05:07:45Z")

</div>

On 20 Nov 2007, at 00:33, tom\_302 wrote:

> When I tried an example binding a date column to Time.now, the time  
> didn’t change with each query. Instead, it continued using the  
> initial value of Time.now from when the class loaded.

You can interpolate these things, but as you found

has\_many :foos, :conditions =\> “#{xyz}”  
doesn’t work: this statement is ran at class load and so the string is  
interpolated

has\_many :foos, :conditions =\> ‘#{xyz}’

should do the trick.

Fred

---

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [November 27, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/association-conditions-instance-variable-evaluation/120718/3 "2007-11-27T16:28:42Z")

</div>

> has\_many :foos, :conditions =\> ‘#{xyz}’

Here’ my problem with the interpolation:

:conditions =\> ‘allow\_spatial\_resampling =  
#{instance\_var\_that\_evals\_to\_true}’

# =\> WHERE (allow\_spatial\_resampling = true)

Great, that works for MySQL, but not, say, SQLite. I need to do:

:conditions =\> [‘allow\_spatial\_resampling = ?’,  
instance\_var\_that\_evals\_to\_true]

# =\> MySQL: WHERE (allow\_spatial\_resampling = 1)

# =\> SQLite: WHERE (allow\_spatial\_resampling = ‘t’)

How can I have a DB agnostic solution, where obviously the latter won’t  
work at the class level.

---

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [November 27, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/association-conditions-instance-variable-evaluation/120718/4 "2007-11-27T16:51:37Z")

</div>

> How can I have a DB agnostic solution, where obviously the latter won’t  
> work at the class level.

At least for my example, I found the solution:

:conditions =\> ‘allow\_spatial\_resampling =  
#{quote\_value(instance\_var)}’

That works as expected, hope this helps someone out there.
