# Issue with Observers and has\_one association

**URL:** <https://www.ruby-forum.com/t/issue-with-observers-and-has-one-association/129580>\
**Category:** Rails\
**Created:** [February 19, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/issue-with-observers-and-has-one-association/129580 "2008-02-19T20:50:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [February 19, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/issue-with-observers-and-has-one-association/129580/1 "2008-02-19T20:50:26Z")

</div>

Hey Guys,

I’m getting a peculiar bug working with Observers and has\_one  
associations. When a has\_one association is invoked by an Observer it  
always returns null. I looked in the development.log file and the SQL  
for the has\_one association isn’t even being executed. On the other  
hand, when I replace the has\_one association with just a hand crafted  
method, it does get called correctly. So the following does not work:

class Task

has\_one :current\_instance,  
:class\_name =\> ‘TaskInstance’,  
:foreign\_key =\> ‘task\_id’,  
:order =\> ‘start\_at DESC’,  
:conditions =\> ‘end\_at IS NULL’

but when replaced with the follwoing, it does work.

def current\_instance  
TaskInstance.find(:first,  
:conditions=\>“task\_id=#{self.id} AND end\_at IS  
NULL”,  
:order=\>‘start\_at DESC’  
)  
end

I’m working with Rails 2.0.2 and the problem doesn’t affect the  
belongs\_to nor the has\_many associations. I’d hate to replace my  
lovely has\_one associations with hand-crafted methods…

Sonny.
