We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
Has anything changed in Rails 5?
Amazing blog post, btw, it's and indispensable reference for me.
Thanks!
Love this sheet.
One thing that may be an addition is the updates that come via collections. For instance a has_many : abc where object.delete(abc) does a direct SQL update (nulls the associated id) and appears to bypass callbacks. That just bit me because the update was (of course) not versioned by PaperTrail gem.
Notice: I think in rails 4.? #write_attribute is renamed to #raw_write_attribute, haven't tried on 4.2.x yet
Nice post! thanks!!!
Really great post. Thanks for sharing. What are your thoughts on including the 'cheat sheet' section in the official rails guide? Also, for greater visibility, try submitting to Ruby Weekly (great channel for leads).
Thanks this is a very good post. I couldn't figure out what had changed since Rails 3.
Something new in Rails 6 in 2020?
Thank you for sharing this.
Thanks so much for this, but please can you add update_attributes to the table? As an alias for update, it has a subtle difference to update_attribute that took me unawares.
That you for this article, 6 years since it was written and its still useful :-D. The cheat sheet is what helped me the most.
update for Rails 5?? <3 <3
very helpful post... :)
Suggest an extra column in the cheat sheet, for which methods permit nested attributes.
Hi, I found a bug in the article. `write_attribute` is private and inaccessible in Rails 4. Thanks!
Muchas gracias! :D
Thank you
Thank you very much!
This keeps saving me :)
Great article!! Bookmarked
Thank you, very good post
very helpful! Thanks
Thanks so much.
Excellent post, Thanks!
just wanted to say thanks, i visit this page a couple times a week
It appears as though `assign_attributes` and `attributes=` actually differ.
I'm seeing `assign_attributes` assign virtual attributes successfully, where `attributes=` does not. Does that seem right?
Never mind. It's literally an alias in Rails 4; should be identical.
Thanks for putting this together :)
Thank you, thank you for this resource. I'm still in the process of learning Rails and this is a great resource and overview.
great article! very helpful
thanks for sharing !!!
Hi,
Is there anyway to stop any other changed attributes from saving to the database when using update_attribute method?
Hey Viduranga,
Nope there isn't. If you check out the source code of the update_attribute method, you'll see that eventually the #save method is called, which saves all changed attributes.
Thanks great post. I will start following you.
lovely thanks for cheat sheet :)
assign_attributes also sets the _was fields! If you use "model.assign_attributes username: 'cow'" then "model.username_was" is also set to "cow"! This happens without even saving. What good are the _was fields if they are changed immediately upon setting the attributes?
It only happens with #clone
def update
@user = current_user.clone # use @user for short
puts "*** #{current_user.username} #{current_user.username_was}"
@user.assign_attributes(user_params) # store the form so changes are not lost while editing
puts "*** #{current_user.username} #{current_user.username_was}" # Both are updated!
*** test39 test39
*** test3944 test3944
Thanks for the add-on. Good addition.
awesome points thank you very much
Nice point of view. Great work.
thanks, it's very useful.