# Polymorphic association and saving a has\_one association

**URL:** <https://www.ruby-forum.com/t/polymorphic-association-and-saving-a-has-one-association/92529>\
**Category:** Rails\
**Created:** [April 20, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/polymorphic-association-and-saving-a-has-one-association/92529 "2007-04-20T12:01:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [April 20, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/polymorphic-association-and-saving-a-has-one-association/92529/1 "2007-04-20T12:01:13Z")

</div>

Hi,

I’ve got a little problem with a polymorphic association and has\_one.

Here’s my current setup:

class Client  
has\_one :birthday, :dependent =\> :destroy, :as =\> :timeable,  
:class\_name  
=\> “Event”, :foreign\_key =\> :timeable\_id  
end

class Event  
belongs\_to :timeable, :polymorphic =\> true  
end

Now I try to edit an existing client and try to save the associated  
event:

def update  
@client = Client.find(params[:id])  
@client.build\_birthday(params[:birthday])  
…  
end

Saving this @client Objekt created a new entry in the Event  
Databasetable. I  
thought, it would overwrite the existing entry in the Event-table, as  
it’s  
a “has\_one” association and not a “has\_many”.

Of course I could write something like (this is working!):

def update  
@client = Client.find(params[:id])  
@client.birthday = Event.new(params[:birthday])  
…  
end

but I would like to avoid the word “Event” in the update method, as it’s  
the  
only appearance of the name of the class, as I try to put the above  
stuff  
including the Event class into a plugin.

So, why does @client.build\_birthday adds a new object and what’s the  
correct  
way of assigning an instance object to a has\_one association?

Regards,  
Timo
