模型中的after_create操作是不是更新父模型的属性?

时间:2012-03-12 10:13:27

标签: ruby-on-rails

我有发布评论模型。

我想要实现以下目标:每次创建或评论帖子时,帖子的:content_changed_at属性都应使用created_at更新:

class Post < ActiveRecord::Base
  attr_accessible :title, :content, :tag_names, :content_changed_at

  has_many :comments, :dependent => :destroy

  def init_sort_column
    self.content_changed_at = self.created_at || Time.now
  end
end

class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id

  belongs_to :post, :counter_cache => true

  after_create :update_parent_sort_column

  private

  def update_parent_sort_column
    if self.post
      self.post.content_changed_at = self.created_at
    end
  end
end
创建帖子时

content_changed_at正在更新(其初始created_at日期)。但由于某种原因,当评论帖子时,该属性不会更新。

这是发布评论的方式:

comments_controller.rb:

  def create
    @post = Post.find(params[:post_id])
    comment_attr = params[:comment].merge :user_id => current_user.id
    @comment = @post.comments.create(comment_attr)
    redirect_to post_path(@post)
  end

创建帖子时生成的SQL:

#<Post id: 64, title: "tttttttttest", content: "hello", user_id: nil, created_at: "2012-03-12 10:07:29", updated_at: "2012-03-12 10:07:29", comments_count: 0, total_votes: 0, content_changed_at: "2012-03-12 10:07:29">

现在一切正常:content_changed_at: "2012-03-12 10:07:29

为帖子创建评论时生成的SQL:

(0.2ms)  SAVEPOINT active_record_1
  SQL (1.5ms)  INSERT INTO "comments" ("content", "created_at", "post_id", "total_votes", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)  [["content", "teststsetsetset"], ["created_at", Mon, 12 Mar 2012 10:10:09 UTC +00:00], ["post_id", 64], ["total_votes", 0], ["updated_at", Mon, 12 Mar 2012 10:10:09 UTC +00:00], ["user_id", nil]]
  Post Load (0.4ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = 64 LIMIT 1
  SQL (0.4ms)  UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1 WHERE "posts"."id" = 64
   (0.2ms)  RELEASE SAVEPOINT active_record_1
 => #<Comment id: 51, content: "teststsetsetset", post_id: 64, user_id: nil, created_at: "2012-03-12 10:10:09", updated_at: "2012-03-12 10:10:09", total_votes: 0> 

现在如果我再次查看帖子:

content_changed_at: "2012-03-12 10:07:29

日期相同。

有任何解决此问题的建议吗?

3 个答案:

答案 0 :(得分:5)

update_parent_sort_column方法中,您更改了帖子的content_changed_at,但实际上并未保存。试着这样做。

def update_parent_sort_column
    if self.post
      self.post.content_changed_at = self.created_at
      self.post.save
    end
  end

或使用update_attribute方法仅更改帖子的一列。

答案 1 :(得分:1)

  class Comment < ActiveRecord::Base
  ...

    def update_parent_sort_column
      if post
        self.post.content_changed_at = self.created_at
      end
    end
  end

在此方法中,您忘记了self.post.save以应用更改。

如果你想在Post模型中跳过回调,你也可以尝试update_column

class Comment < ActiveRecord::Base
  ...

  private
  def update_parent_sort_column
    if post
      self.post.update_column(:content_changed_at, self.created_at)
    end
  end
end

答案 2 :(得分:0)

您还可以在评论模型中尝试以下操作:

class Comment < ActiveRecord::Base
attr_accessible :content, :user_id

belongs_to :post, :counter_cache => true, :touch => :content_changed_at
...
保存或销毁touch后,

content_changed_at会更新相关Post模型字段的Comment