# Assigning default values to attributes in a has\_one associat

**URL:** <https://www.ruby-forum.com/t/assigning-default-values-to-attributes-in-a-has-one-associat/103617>\
**Category:** Rails\
**Created:** [May 8, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/assigning-default-values-to-attributes-in-a-has-one-associat/103617 "2007-05-08T14:51:23Z")\
**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:** [May 8, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/assigning-default-values-to-attributes-in-a-has-one-associat/103617/1 "2007-05-08T14:51:23Z")

</div>

Hi List,

I’ve a little problem here, for which I can’t find a solution:

I have two models, person and event. Event is a polymorphic association  
(timeable). In my person model I would like to define an association  
with  
Events:

class Person \< ActiveRecord::Base

```
has_one birthday,
    :dependent => :destroy,
    :as => :timeable,
    :class_name => "Event",
    :foreign_key => :timeable_id,
    :conditions => "category = 'birthday'"

```

end

As you can see, there’s a category field in the event table, which  
should  
contain the category of the Event. In this case, is should  
contain “birthday”.

I would like this category attribute to get set automatically to  
“birthday”  
whenever I create a new association.

So, calling simething like

@client.build\_birthday(params[:birthday])

in my controller should set the category attribute to “birthday”. I was  
trying  
something like:

```
has_one birthday,
    :dependent => :destroy,
    :as => :timeable,
    :class_name => "Event",
    :foreign_key => :timeable_id,
    :conditions => ['category = ?', name.to_s] do
        self.category = "birthday"
    end

```

But this seems not to work.

Also there are callbacks for collection associations (i.e. before\_add  
and  
after\_add), but this does not work with has\_one associations.

So, what would be the best way of doing womthing like this (I sure there  
is at  
least _one_ way…)

Regards,  
Timo
