We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

feixiong • 11 years ago

thanks, it's very useful.

Jonny_eh • 9 years ago

Has anything changed in Rails 5?

Amazing blog post, btw, it's and indispensable reference for me.

Wellington T. • 8 years ago

Thanks!

tjchambers • 8 years ago

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.

Leung Ho Kuen • 10 years ago

Notice: I think in rails 4.? #write_attribute is renamed to #raw_write_attribute, haven't tried on 4.2.x yet

Fon • 11 years ago

Nice post! thanks!!!

NoviceCodeNinja • 11 years ago

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).

Troy Martin • 11 years ago

Thanks this is a very good post. I couldn't figure out what had changed since Rails 3.

woto • 4 years ago

Something new in Rails 6 in 2020?

Élisson Michael • 4 years ago

Thank you for sharing this.

Leslie Viljoen • 5 years ago

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.

Harisankar P S • 5 years ago

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.

Ruberto Paulo • 7 years ago

update for Rails 5?? <3 <3

neymarsabin • 8 years ago

very helpful post... :)

inopinatus • 8 years ago

Suggest an extra column in the cheat sheet, for which methods permit nested attributes.

Max • 8 years ago

Hi, I found a bug in the article. `write_attribute` is private and inaccessible in Rails 4. Thanks!

Comentarista • 8 years ago

Muchas gracias! :D

Leandrit Ferizi • 8 years ago

Thank you

Francisco Quintero • 8 years ago

Thank you very much!

br3nt • 9 years ago

This keeps saving me :)

Mirzalazuardi • 9 years ago

Great article!! Bookmarked

Youcef Dahmani • 9 years ago

Thank you, very good post

Lujia • 9 years ago

very helpful! Thanks

Rafael Pereira • 9 years ago

Thanks so much.

Joshua Harris • 9 years ago

Excellent post, Thanks!

Damien Jones • 9 years ago

just wanted to say thanks, i visit this page a couple times a week

Katy • 9 years ago

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?

Katy • 9 years ago

Never mind. It's literally an alias in Rails 4; should be identical.

funkdified • 10 years ago

Thanks for putting this together :)

Phil Birnie • 10 years ago

Thank you, thank you for this resource. I'm still in the process of learning Rails and this is a great resource and overview.

Casey Robinson • 10 years ago

great article! very helpful

ferdinandrosario • 10 years ago

thanks for sharing !!!

Viduranga Wijesooriya • 11 years ago

Hi,
Is there anyway to stop any other changed attributes from saving to the database when using update_attribute method?

David • 11 years ago

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.

goodbedford • 11 years ago

Thanks great post. I will start following you.

Gourav Tiwari • 11 years ago

lovely thanks for cheat sheet :)

Danny • 11 years ago

David Love the cheat sheet. Thanks a lot!

starrychloe • 11 years ago

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

goodbedford • 11 years ago

Thanks for the add-on. Good addition.

rakesh verma • 11 years ago

awesome points thank you very much

Jorge Díaz • 11 years ago

Nice point of view. Great work.