开发者

How to know whether a model is new or not?

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 databas开发者_StackOverflow中文版e?


post.new_record?


ActiveRecord's new_record? method returns true if the object hasn't been saved yet.

  • new_record? documentation


you can use post.persisted? as well, if it return false means record in new

persisted?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜