Skip to content

Commit

Permalink
prefix position_column w/ table name in higher/lower item queries
Browse files Browse the repository at this point in the history
Joining the relation returned by `#higher_items` or `#lower_items`
to a table also having `position_column` would result in an
ambiguous column error
  • Loading branch information
Nathaniel Williams committed May 2, 2016
1 parent a3b1d4e commit 8ef09b4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ def higher_items(limit=nil)
limit ||= acts_as_list_list.count
position_value = send(position_column)
acts_as_list_list.
where("#{position_column} < ?", position_value).
where("#{position_column} >= ?", position_value - limit).
where("#{acts_as_list_class.table_name}.#{position_column} < ?", position_value).
where("#{acts_as_list_class.table_name}.#{position_column} >= ?", position_value - limit).
limit(limit).
order("#{quoted_table_name}.#{quoted_position_column} ASC")
end
Expand All @@ -273,8 +273,8 @@ def lower_items(limit=nil)
limit ||= acts_as_list_list.count
position_value = send(position_column)
acts_as_list_list.
where("#{position_column} > ?", position_value).
where("#{position_column} <= ?", position_value + limit).
where("#{acts_as_list_class.table_name}.#{position_column} > ?", position_value).
where("#{acts_as_list_class.table_name}.#{position_column} <= ?", position_value + limit).
limit(limit).
order("#{quoted_table_name}.#{quoted_position_column} ASC")
end
Expand Down
64 changes: 64 additions & 0 deletions test/test_joined_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'helper'

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.verbose = false

class Section < ActiveRecord::Base
has_many :items
acts_as_list

scope :visible, -> { where(visible: true) }
end

class Item < ActiveRecord::Base
belongs_to :section
acts_as_list scope: :section

scope :visible, -> { where(visible: true).joins(:section).merge(Section.visible) }
end

class JoinedTestCase < Minitest::Test
def setup
ActiveRecord::Base.connection.create_table :sections do |t|
t.column :position, :integer
t.column :visible, :boolean, default: true
end

ActiveRecord::Base.connection.create_table :items do |t|
t.column :position, :integer
t.column :section_id, :integer
t.column :visible, :boolean, default: true
end

ActiveRecord::Base.connection.schema_cache.clear!
[Section, Item].each(&:reset_column_information)
super
end

def teardown
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.drop_table(table)
end
super
end
end

# joining the relation returned by `#higher_items` or `#lower_items` to another table
# previously could result in ambiguous column names in the query
class TestHigherLowerItems < JoinedTestCase
def test_higher_items
section = Section.create
item1 = Item.create section: section
item2 = Item.create section: section
item3 = Item.create section: section
assert_equal item3.higher_items.visible, [item1, item2]
end

def test_lower_items
section = Section.create
item1 = Item.create section: section
item2 = Item.create section: section
item3 = Item.create section: section
assert_equal item1.lower_items.visible, [item2, item3]
end
end

0 comments on commit 8ef09b4

Please sign in to comment.