user.name after running this code?user = User.create(name: "Alice", age: 30) user.name
create method saves the record and returns the object with attributes set.The create method in Rails creates and saves a new record. The returned object has the attributes assigned, so user.name will be "Alice".
user.persisted? after new and save?user.persisted? return after the save call?user = User.new(name: "Bob")
saved = user.save
user.persisted?save method returns true if the record was saved successfully.After calling save on a new record, if it succeeds, the record is persisted in the database. Thus, user.persisted? returns true.
user.update(name: nil) raise an error if the model validates presence of name?user = User.find(1)
user.update(name: nil)update or update! is used and how validations behave.The update method returns false if validations fail but does not raise an error. Only update! raises an exception on validation failure.
destroy deletes the record and runs callbacks. delete deletes without callbacks. remove is not a valid method. destroy_by deletes by conditions but returns the number of records deleted.
update and reload?user.name be after the last line?user = User.create(name: "Carol") user.update(name: "Diana") user.reload user.name
reload method refreshes the object from the database.The update changes the name to "Diana" in the database. reload refreshes the object with the latest data, so user.name is "Diana".
