Defect #21581

Statement invalid error on including default scope by patching a model.

Added by Rupesh J almost 2 years ago. Updated over 1 year ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:-
Target version:-
Resolution: Affected version:

Description

In plugin development, It is not possible to add the default scope by patching any models.

def self.included(base) # :nodoc:
        base.extend(ClassMethods)
        base.send(:include, InstanceMethods)

        base.class_eval do
           default_scope -> { includes(:table_name) }
....

The tests fail with Statement Invalid.

IssueNestedSetTest#test_move_a_root_to_child:
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'id' in where clause is ambiguous:
 SELECT MAX(`issues`.`rgt`) FROM `issues` LEFT OUTER JOIN `egs` ON `egs`.`issue_id` = `issues`.`id`
 WHERE `issues`.`root_id` = 1372 AND `issues`.`parent_id` = 1372 AND (id < 1373)

I guess, the following is the culprit, issue_nested_set.rb

      def target_lft
        scope_for_max_rgt = self.class.where(:root_id => root_id).where(:parent_id => parent_id)
        if id
          scope_for_max_rgt = scope_for_max_rgt.where("id < ?", id)

Can you please look into this issue.

Happens in Redmine 3.0.3
Ruby 2.1

History

#1 Updated by Toshi MARUYAMA over 1 year ago

  • Description updated (diff)

#2 Updated by Toshi MARUYAMA over 1 year ago

  • Description updated (diff)

#4 Updated by Toshi MARUYAMA over 1 year ago

  • Status changed from New to Needs feedback

#5 Updated by Rupesh J over 1 year ago

Thank You very much.
Issue resolved.

#6 Updated by Toshi MARUYAMA over 1 year ago

  • Status changed from Needs feedback to Closed

Thank you for your feedback.

Also available in: Atom PDF