# Has\_one association with :conditions: is this somehow possible?

**URL:** <https://www.ruby-forum.com/t/has-one-association-with-is-this-somehow-possible/131734>\
**Category:** Rails\
**Created:** [March 12, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/has-one-association-with-is-this-somehow-possible/131734 "2008-03-12T20:55:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Feurio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3bc359/32.png) [@Feurio](https://www.ruby-forum.com/u/Feurio)\
**Post date:** [March 12, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/has-one-association-with-is-this-somehow-possible/131734/1 "2008-03-12T20:55:41Z")

</div>

What is the right way to achieve something like this:

class Design \< ActiveRecord::Base  
belongs\_to :card\_collection  
after\_save :generate\_images

has\_one :background,  
:as =\> :resourceable,  
:class\_name =\> “Resource”,  
:conditions =\> [‘title = ?’, self.id],  
:dependent =\> :destroy  
end

* * *

Everything here is working as expected. ONLY the conditions part is  
not working…

The problem is that self.id is NOT the @my\_design.id (the id of the  
active record design instance), but the object\_id of the instance. HOW  
can I tell ActiveRecord that I want the @my\_design.id ???

Thanks for any help!

Feurio

---

<div class="post-metadata">

**Author:** ![Feurio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3bc359/32.png) [@Feurio](https://www.ruby-forum.com/u/Feurio)\
**Post date:** [March 12, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/has-one-association-with-is-this-somehow-possible/131734/2 "2008-03-12T21:11:04Z")

</div>

On 12 Mar 2008, at 19:54, Feurio wrote:

> ```
> :conditions => ['title = ?', self.id],
> 
> ```

Set conditions to ‘title =#{self.id}’ (make sure it’s single quotes  
not double quotes). Activerecord should interpolate it for you.

Fred

---

<div class="post-metadata">

**Author:** ![Feurio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3bc359/32.png) [@Feurio](https://www.ruby-forum.com/u/Feurio)\
**Post date:** [March 16, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/has-one-association-with-is-this-somehow-possible/131734/3 "2008-03-16T17:51:52Z")

</div>

Hi Fred,

thanks for the hint. I finally found out how to get it work:

* * *

class Design \< ActiveRecord::Base  
belongs\_to :card\_collection  
after\_save :generate\_images

has\_one :background,  
:as =\> :resourceable,  
:class\_name =\> “Resource”,  
:conditions =\> [‘title = ?’, ‘#{self.id}’],  
:dependent =\> :destroy  
end

* * *

…by putting it in single quotes it worked.

Thanks.

Feurio

On 12 Mrz., 21:08, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
