Yu-Chieh’s Blog (Y.C. Chang)

Ruby on Rails / Rubygems / FullStack / Git / Mac notes.

Rails Tip: How to Update Attributes Without Save

  • generally, when we use update_attributes, it will save automatically. however, what if we just want to new an record and update some attributes of it. what can we do?

  • We can use assign_attributes method.

1
2
3
4
user = User.new
user.assign_attributes({ :name => 'Andy'})
user.name        # => "Andy"
user.new_record? # => true

Comments