如何检查ActiveRecord对象是新的还是已经持久化了?
一个ActiveRecord对象生命周期:
1.新纪录
item = Item.new
item.new_record? #=> true
2.坚持
item.save
item.persisted? #=> true
3.改变了
item.name = "other"
item.changed? #=> true
4.破坏
item.destroy
item.destroyed? #=> true
推荐文章
- 如何确定一个数组是否包含另一个数组的所有元素
- Rails find_or_create_by多个属性?
- Rails ActiveRecord日期之间
- 是什么导致这个ActiveRecord::ReadOnlyRecord错误?
- Rails:dependent =>:destroy VS:dependent =>:delete_all
- 对于PostgreSQL表来说,多大才算太大?
- Rails:如何在Rails 4中引用CSS中的图像
- 如何使用RSpec的should_raise与任何类型的异常?
- Rails -嵌套包括活动记录?
- Delete_all vs destroy_all
- Rails获取“each”循环的索引
- 在rails redirect_to中传递参数
- Rails new vs create
- Rails has_many的别名
- Ruby:包含的反义词是什么?Ruby数组?