Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

ruby on rails - Determine if ActiveRecord Object is New

How can I check if an ActiveRecord object is new or is already persisted?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An ActiveRecord object lifecycle:

1.new record

item = Item.new
item.new_record? #=> true

2.persisted

item.save
item.persisted? #=> true

3.changed

item.name = "other"
item.changed? #=> true

4.destroyed

item.destroy
item.destroyed? #=> true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...