AdamNYC
AdamNYC

Reputation: 20415

Callback method after a new object is initialized but not after find

I have a Secret model that has attribute token. I need to generate this token after the object is initialized for the first time, i.e., after Secret.new, but not after Secret.find

I couldn't find any callback that fits my purpose. It seems that after_initialize excluding after_find would have work.

How should I deal with this situation? Am I doing something smelly that Rails wasn't prepared for?

Upvotes: 0

Views: 313

Answers (1)

steakchaser
steakchaser

Reputation: 5239

In the after_initialize callback you could check to see if the record is new or not via secret.new_record? Then, only generate your token for new records.

Upvotes: 1

Related Questions