trnc
trnc

Reputation: 21577

Setting model value conditional in rails?

i have a model with the following field

when updating the model, i want to set published_at to Time.now when published is set true!

any advice how i can achieve this? thanks!

Upvotes: 0

Views: 110

Answers (1)

Luis D Urraca
Luis D Urraca

Reputation: 2084

In your model you need to put this

before_save :set_published_at

def set_published_at
    @published_at = Time.now if @published == true
end

Upvotes: 1

Related Questions