2014-05-06 22 views
1

試圖將Rails從V4.0.4升級到4.1.0。在評論模型此語句導致運行時錯誤試圖從Rails 4.0.4升級到4.1.0 gem'acts_as_commentable_with_threading'導致錯誤

評論型號

class Comment < ActiveRecord::Base 
    include ActiveModel::ForbiddenAttributesProtection 

    # added by me so comments_on_comments sorted appropriately 
    default_scope { order(:updated_at => :desc) } 

    # causes error in rails 4.1.0 
    acts_as_nested_set :scope => [:commentable_id, :commentable_type] 

    validates :body, :presence => true 
    validates :user, :presence => true 
    ... 

的Gemfile

source 'https://rubygems.org' 

ruby '2.1.1' 
... 
gem 'acts_as_commentable_with_threading', '~> 1.2.0' 

原因這個錯誤在開發日誌

Unknown key: :order. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache 
acts_as_nested_set :scope => [:commentable_id, :commentable_type] -- is highlighted 

註釋掉「acts_as_nested_set:範圍=> [:commentable_id,:commentable_type]「,然後導致這個錯誤在選擇一篇文章時(對文章有評論)。

undefined method `children' for #<Comment:0x00000107260040> 

回答

0

這可能與awesome_nested_set中不贊成使用的語法有關。

DEPRECATION WARNING: The following options in your Comment.has_many :children declaration are deprecated: :order. Please use a scope block instead. For example, the following: 

    has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment' 

should be rewritten as the following: 

    has_many :spam_comments, -> { where spam: true }, class_name: 'Comment' 
. (called from <class:Comment> at /Users/reconstructions/Desktop/Coding 2013/Rails Apps/sfrc/app/models/comment.rb:2) 

線造成這種錯誤是與上述相同的一個,並且在該範圍的語法是陳舊的:使用acts_as_commentable_with_threading寶石v.2.1.6當我在RSPEC得到這個錯誤。

碰到gem "awesome_nested_set", '~> 3.0.0.rc.3'爲我工作,所以上面的答案是正確的。如果任何人不能或不想碰到那個版本,修正這個語法錯誤可能是一個開始的地方。