# Access an attribute in the find method

**URL:** <https://www.ruby-forum.com/t/access-an-attribute-in-the-find-method/163042>\
**Category:** Rails\
**Created:** [March 16, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/access-an-attribute-in-the-find-method/163042 "2009-03-16T23:05:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [March 16, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/access-an-attribute-in-the-find-method/163042/1 "2009-03-16T23:05:14Z")

</div>

Hello everyone 🙂

I need to access an attribute directly into a find. Is that possible?

For instance:

Topics.find(:all, :conditions =\> expiration\_date-1.hour + " \< NOW( )")

Here, expiration\_date would be an attribute of Topics.

Thank you !

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [March 16, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/access-an-attribute-in-the-find-method/163042/2 "2009-03-16T23:22:41Z")

</div>

I think you’d do something like this:

Topics.find(:all, :conditions =\> [“expiration\_date \< ?”, Time.now +  
1.hour])

See  
[http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001891&name=findfor](http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001891&name=findfor)  
more examples.

Regards,  
Craig

–  
Craig D.  
Mutually Human Software

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com/)**
>
> Technology Made for Humans by humans Data Analytics Data Analytics Data has become a critical part of business for most organizations. We work with you to ensure you have confidence in your data, can properly access it, and present it in a way that...

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [March 16, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/access-an-attribute-in-the-find-method/163042/3 "2009-03-16T23:49:17Z")

</div>

It helps, thanks!

But what if I have 2 conditions ?

For instance, is this possible?

Topics.find(:all, :conditions =\> [“expiration\_date \< ? AND attribute =”,  
Time.now + 1.hours], yet\_another\_variable)

Thank you!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [March 17, 2009, 12:15am UTC](https://www.ruby-forum.com/t/access-an-attribute-in-the-find-method/163042/4 "2009-03-17T00:15:26Z")

</div>

Topic.find(:all, :conditions =\> [“expiration\_date \< ? AND attribute =  
?”,  
Time.now + 1.hour, yet\_another\_variable])

In other words, everything goes in the conditions array. The first  
element  
is the SQL fragment. Every other element is a value to be substituted  
for a  
? in the SQL fragment. Rails will properly quote strings and dates.

Again, see the find docs [  
[http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001891&name=find](http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M001891&name=find)]  
for examples. Also, the Rails Guides [  
[http://guides.rubyonrails.org](http://guides.rubyonrails.org) ] are good, and the section on query  
conditions [  
[Active Record Query Interface — Ruby on Rails Guides](http://guides.rubyonrails.org/active_record_querying.html#conditions) ]  
has  
more details.

Regards,  
Craig

–  
Craig D.  
Mutually Human Software

> **[Homepage | Mutually Human](https://www.mutuallyhuman.com/)**
>
> Technology Made for Humans by humans Data Analytics Data Analytics Data has become a critical part of business for most organizations. We work with you to ensure you have confidence in your data, can properly access it, and present it in a way that...
