-
-
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 and I'm sure many others, including different ways to insert linebreaks
same can be said for saving itself. do you write custom methods handling saving/updating/creating?
just seems like extra work to me [presuming saving future extra work] and i'm lazy.
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
@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.
You forgot
update_attributes!. ;)