Best way to represent who changed the state of an object and when? (AASM)
Right now I'm storing the user who last updated the state of my model in the state_last_updated_by_id
field and the time the state was last updated in the state_updated_at
field. Then I define methods like this:
def published_at
return unless state == 'published'
state_updated_at
end
def published_by
return unless state == 'published'
state_last_updated_by
end
def nominated_for_publishing_at
return unless state == 'under_review'
state_updated_at
end
def nominated_fo开发者_如何学JAVAr_publishing_by
return unless state == 'under_review'
state_last_updated_by
end
This clearly won't scale (and isn't a great approach to begin with) -- what's the right way to do this?
Have you looked into the acts_as_audited plugin?
It's very simple to set up, and will record the timestamp, changes and the user id of the person who triggers a record update/save in a related table. Making retrieval of the last user to change state very simple. It can also be configured to only trigger when the state is changed.
However it's probably less scalable than your current method of doing things because it will keep a record of every time the state of your model is changed.
精彩评论