Polymorphic association and saving a has_one association

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs