Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 按最新创建的注释排序_Ruby On Rails_Ruby On Rails 3_Comments - Fatal编程技术网

Ruby on rails 按最新创建的注释排序

Ruby on rails 按最新创建的注释排序,ruby-on-rails,ruby-on-rails-3,comments,Ruby On Rails,Ruby On Rails 3,Comments,我在我的主题表中创建了一个“active”字段,我可以使用该字段显示活动主题,该字段将包含首次创建主题时的内容,当有人发表评论时,它将使用comment.created\u,并将其放在主题表的活动字段中,和其他论坛系统一样 我在这里发现了类似的问题 但是它对我不起作用,我不知道为什么它不起作用。我也不知道在这种情况下是否需要使用计数器缓存。我对我的评论使用多态关联,因此我不确定如何使用计数器缓存。在我的主题表中,可以将创建的_复制到活动字段。但当我创建一条评论时,它不会起作用 错误: Comm

我在我的主题表中创建了一个“active”字段,我可以使用该字段显示活动主题,该字段将包含首次创建主题时的内容,当有人发表评论时,它将使用comment.created\u,并将其放在主题表的活动字段中,和其他论坛系统一样

我在这里发现了类似的问题

但是它对我不起作用,我不知道为什么它不起作用。我也不知道在这种情况下是否需要使用计数器缓存。我对我的评论使用多态关联,因此我不确定如何使用计数器缓存。在我的主题表中,可以将创建的_复制到活动字段。但当我创建一条评论时,它不会起作用

错误:

CommentsController中的命名错误#创建

未定义的方法“topic”

Topic.rb

class Topic < ActiveRecord::Base
  attr_accessible :body, :forum_id, :title

  before_create :init_sort_column

  belongs_to :user
  belongs_to :forum
  validates :forum_id, :body, :title, presence: true

  has_many :comments, :as => :commentable

  default_scope order: 'topics.created_at DESC'

  private
  def init_sort_column
    self.active = self.created_at || Time.now
  end
end
class Comment < ActiveRecord::Base
  attr_accessible :body, :commentable_id, :commentable_type, :user_id

  belongs_to :user
  belongs_to :commentable, :polymorphic => true

  before_create :update_parent_sort_column

  private

  def update_parent_sort_column
    self.topic.active = self.created_at if self.topic
  end

end
类主题:可评论
默认范围顺序:“topics.created\u at DESC”
私有的
def init_sort_列
self.active=self.created_在| | Time.now
结束
结束
Comment.rb

class Topic < ActiveRecord::Base
  attr_accessible :body, :forum_id, :title

  before_create :init_sort_column

  belongs_to :user
  belongs_to :forum
  validates :forum_id, :body, :title, presence: true

  has_many :comments, :as => :commentable

  default_scope order: 'topics.created_at DESC'

  private
  def init_sort_column
    self.active = self.created_at || Time.now
  end
end
class Comment < ActiveRecord::Base
  attr_accessible :body, :commentable_id, :commentable_type, :user_id

  belongs_to :user
  belongs_to :commentable, :polymorphic => true

  before_create :update_parent_sort_column

  private

  def update_parent_sort_column
    self.topic.active = self.created_at if self.topic
  end

end
class注释true
创建前:更新父列排序列
私有的
def update_parent_sort_列

self.topic.active=self.created\u在if self.topic 结束 结束
没有意识到您使用的是多态关联。使用以下命令:

def update_parent_sort_column
  commentable.active = created_at if commentable.is_a?(Topic)
  commentable.save!
end

应该这样做。

为什么要将
注释的
处创建的
设置为
主题的
活动的
值?是否要将
topic.active
设置为
Comment
created_处?self.topic.active=self.created_处如果self.topic,尝试了此操作,我仍然会收到相同的错误“未定义的方法'topic'”这不会产生错误,但不会更新活动字段。编辑之前的字段和现在的字段都是。哎呀,忘了添加
保存