Setting an attribute when building or creating?

Look into using callbacks, e.g. after_create or before_create (define
the methods and they get fired as you'd expect)

You could check for the presence of the category id in such a
callback, and then do your key stuff.