How to know whether a model is new or not?
Ruby on-RailsRubyModelRuby on-Rails Problem Overview
class Post < ActiveRecord::Base
end
post = Post.new
How do I judge whether the 'post' is a new model which is not pulled from the database?
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
post.new_record?
Solution 2 - Ruby on-Rails
ActiveRecord's new_record?
method returns true
if the object hasn't been saved yet.
Solution 3 - Ruby on-Rails
you can use post.persisted? as well, if it return false means record in new
http://apidock.com/rails/ActiveRecord/Persistence/persisted%3F"> persisted?