Timo Springmann
2007-Apr-20 09:59 UTC
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---