elight/acts_as_commentable_with_threading

ArgumentError: comparison of Fixnum with nil failed

Closed this issue · 2 comments

Hi,
The following is the create action of my comments controller.

def create
        @commentable = find_commentable
        @comment_body, @parent_id = params[:comment][:body], params[:comment][:parent_id].to_i
        @comment = Comment.build_from(@commentable, current_user.id, @comment_body)
        if @comment.save
            if @parent_id > 0
                @parent_comment = Comment.find(@parent_id)
                @comment.move_to_child_of(@parent_comment)
            end
            flash[:notice] = "Your comment is created successfully."
            respond_to do |format|
                format.html { redirect_to @commentable }
                format.js
            end            
        else
            respond_to do |format|
                format.html { render :action => 'new' }
                format.js
            end
        end
    end

The comment gets save but when it tries to move the newly created comment to be child of existing comment, the following error is thrown in development log

ActiveRecord::ActiveRecordError (Impossible move, target node cannot be inside moved tree.):
  app/controllers/comments_controller.rb:31:in `create'

And this is the error when I tried this same thing on console

>> @comment.save
=> true
>> @comment.move_to_child_of(@parent_comment)
ArgumentError: comparison of Fixnum with nil failed
    from /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/whiny_nil.rb:55:in `<='
    from /home/millisami/rails_apps/wnn_finale/vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb:410:in `move_possible?'
    from /home/millisami/rails_apps/wnn_finale/vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb:497:in `move_to'
    from /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
    from /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:182:in `transaction'
    from /opt/ruby-enterprise-1.8.6-20090610/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/transactions.rb:188:in `transaction'
    from /home/millisami/rails_apps/wnn_finale/vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb:488:in `move_to'
    from /home/millisami/rails_apps/wnn_finale/vendor/plugins/awesome_nested_set/lib/awesome_nested_set.rb:397:in `move_to_child_of'
    from (irb):7

What might be the culprit??

The above error "ArgumentError: comparison of Fixnum with nil failed" is fixed after setting up the lft and rgt column default to 0 from NULL.
But now I'm still getting the following error. The one:

ActiveRecord::ActiveRecordError (Impossible move, target node cannot be inside moved tree.):
  app/controllers/comments_controller.rb:31:in `create'

Here is the trace from the log file:

Processing CommentsController#create (for 127.0.0.1 at 2009-08-22 17:57:25) [POST]
  Parameters: {"action"=>"create", "controller"=>"comments", "article_id"=>"testing-authoer-article", "comment"=>{"body"=>"Leaving a reply.", "parent_id"=>"60"}}
  User Columns (3.3ms)   SHOW FIELDS FROM `users`
  User Load Scrooged (1.1ms)   SELECT `users`.salt,`users`.name,`users`.updated_at,`users`.activated_at,`users`.crypted_password,`users`.deleted_at,`users`.remember_token_expires_at,`users`.activation_code,`users`.invitation_id,`users`.password_reset_code,`users`.id,`users`.remember_token,`users`.identity_url,`users`.login,`users`.created_at,`users`.state,`users`.email FROM `users` WHERE (`users`.`id` = 6) LIMIT 1
  Article Load (0.5ms)   SELECT articles.* FROM `articles` INNER JOIN `slugs` ON `slugs`.sluggable_id = `articles`.id AND `slugs`.sluggable_type = 'Article' WHERE (`slugs`.`sequence` = '1' AND `slugs`.`scope` IS NULL AND `slugs`.`name` = 'testing-authoer-article') LIMIT 1
  SQL (0.4ms)   BEGIN
  SQL (1.0ms)   SELECT max(`comments`.rgt) AS max_rgt FROM `comments` WHERE (`comments`.`commentable_id` = 3 AND `comments`.`commentable_type` = 'Article') 
  Comment Create (0.7ms)   INSERT INTO `comments` (`commentable_type`, `updated_at`, `body`, `lft`, `commentable_id`, `user_id`, `parent_id`, `rgt`, `created_at`) VALUES('Article', '2009-08-22 12:12:25', 'Leaving a reply.', 11, 3, 6, NULL, 12, '2009-08-22 12:12:25')
  SQL (5.0ms)   COMMIT
  Comment Load Scrooged (0.7ms)   SELECT `comments`.id FROM `comments` WHERE (`comments`.`id` = 60) 
  SQL (0.4ms)   BEGIN
  Comment Load (0.5ms)   SELECT `lft`, `rgt`, `parent_id` FROM `comments` WHERE (`comments`.`id` = 60) 
  Comment Load (0.7ms)   SELECT `lft`, `rgt`, `parent_id` FROM `comments` WHERE (`comments`.`id` = 62) 
  SQL (0.4ms)   ROLLBACK

ActiveRecord::ActiveRecordError (Impossible move, target node cannot be inside moved tree.):
  app/controllers/comments_controller.rb:31:in `create'

Rendered rescues/_trace (234.2ms)
Rendered rescues/_request_and_response (0.5ms)
Rendering rescues/layout (internal_server_error)

Haven't heard anything here in a while. Closing.