浏览 2029 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-11-18
rails demo && cd demo ruby script/generate model Post title:string body:text lock_version:integer # 注意进入 migrate lock_version 后面加 :default => 0 ruby script/generate model Comment body:text post_id:integer rake db:migrate model里配置 Post has many comments, 代码如下: class Post < ActiveRecord::Base has_many :comments, :dependent => :destroy end class Comment < ActiveRecord::Base belongs_to :post after_destroy :change_post_title! protected def change_post_title! self.post.update_attributes!(:title => "new title") end end 进入console, 执行如下代码: post = Post.new(:title => 'title') post.comments.build(:body => 'comment1') post.comments.build(:body => 'comment1') post.save Post.last.destroy # => ActiveRecord::StaleObjectError: Attempted to delete a stale object Log记录: Post Create (0.2ms) INSERT INTO "posts" ("created_at", "title", "body", "updated_at", "lock_version") VALUES('2009-11-18 09:12:48', 'title', NULL, '2009-11-18 09:12:48', 0) Comment Create (0.1ms) INSERT INTO "comments" ("created_at", "body", "updated_at", "post_id") VALUES('2009-11-18 09:12:48', 'comment1', '2009-11-18 09:12:48', 6) Comment Create (0.1ms) INSERT INTO "comments" ("created_at", "body", "updated_at", "post_id") VALUES('2009-11-18 09:12:48', 'comment1', '2009-11-18 09:12:48', 6) Post Load (0.3ms) SELECT * FROM "posts" ORDER BY posts.id DESC LIMIT 1 Comment Load (0.3ms) SELECT * FROM "comments" WHERE ("comments".post_id = 6) Comment Destroy (0.2ms) DELETE FROM "comments" WHERE "id" = 9 Post Load (0.2ms) SELECT * FROM "posts" WHERE ("posts"."id" = 6) Post Update with optimistic locking (0.1ms) UPDATE "posts" SET "updated_at" = '2009-11-18 09:12:54', "title" = 'new title', "lock_version" = 1 WHERE id = 6 AND "lock_version" = 0 Comment Destroy (0.1ms) DELETE FROM "comments" WHERE "id" = 10 Post Load (0.2ms) SELECT * FROM "posts" WHERE ("posts"."id" = 6) Post Destroy (0.1ms) DELETE FROM "posts" WHERE "id" = 6 AND "lock_version" = 0 通过log记录分析就能明白. 假如主类有lock_version字段做子类的级联删除, Rails 2.3.4 已经加了表记录锁. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |