Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a model object is persisted to DB or a new object?

category = Category.new

This isn't persisted to DB yet, so how to distinguish form an object that is saved in the database?

like image 429
mlzboy Avatar asked Oct 29 '10 08:10

mlzboy


Video Answer


2 Answers

c.persisted?  # => true if persisted
c.new_record? # => true if not persisted

:)

like image 191
Nicolas Blanco Avatar answered Oct 05 '22 03:10

Nicolas Blanco


c.new_record? # true if new, false if saved
like image 26
PeterWong Avatar answered Oct 05 '22 04:10

PeterWong