Skip to content

Instantly share code, notes, and snippets.

@maxim
Created July 28, 2011 15:21
Show Gist options
  • Select an option

  • Save maxim/1111735 to your computer and use it in GitHub Desktop.

Select an option

Save maxim/1111735 to your computer and use it in GitHub Desktop.
# Ways to assign a timestamp:
@user.logged_in_at = Time.current
@user.logged_in_at = Time.now
@user.update_attribute(:logged_in_at, Time.now)
@user.update_attribute(:logged_in_at, Time.current)
@user.update_attributes(:logged_in_at => Time.now)
# Also consider variations of those.
# Or just...
@user.mark_logged_in # this could be named better
@akahn

akahn commented Jul 28, 2011

Copy link
Copy Markdown

You forgot update_attributes!. ;)

@maxim

maxim commented Jul 28, 2011

Copy link
Copy Markdown
Author

@akahn and I'm sure many others, including different ways to insert linebreaks

@rsl

rsl commented Jul 28, 2011

Copy link
Copy Markdown

same can be said for saving itself. do you write custom methods handling saving/updating/creating?

@rsl

rsl commented Jul 28, 2011

Copy link
Copy Markdown

just seems like extra work to me [presuming saving future extra work] and i'm lazy.

@rsl

rsl commented Jul 28, 2011

Copy link
Copy Markdown

oh note that ALL of those feature the string "logged_in_at" which is exactly what you'd be looking for, right? i don't see how hard this is to grep. heh

@maxim

maxim commented Jul 28, 2011

Copy link
Copy Markdown
Author

@rsl It's a lot of headache to grep in a medium-size project since setting an attribute is usually nowhere near as frequent as getting it.

Saving is a bit different. You intend to save, and that's the exact method you're calling. Here, the intention is to mark some event, like "note that user has logged in". How it's noted is a separate story, and having logic scattered all over the place implementing the method by which something is noted introduces problems which can all be avoided with practically no overhead. I've just happened to be bitten by it more than once, but there are certainly cases where it could be unnecessary. It just became a second nature to me, a "rule of thumb API practice" if you will.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment