How do you update Rails model attributes without saving?
For more than one attribute, use the assign_attributes
method.
irb> movie = Movies.new
=> #<Movie id: nil, title: nil, year: nil, director: nil, created_at: nil, updated_at: nil>
irb> movie.assign_attributes(title: 'The Shawshank Redemption', year: 1994)
=> nil
irb> movie
=> #<Movie id: nil, title: "The Shawshank Redemption", year: 1994, director: nil, created_at: nil, updated_at: nil>
For a single attribute, use the attribute=
method.
irb> movie.director = 'Frank Darabont'
=> "Frank Darabont"
irb> movie
=> #<Movie id: nil, title: "The Shawshank Redemption", year: 2004, director: "Frank Darabont", created_at: nil, updated_at: nil>