Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Sort with existing join on same table #935

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def build_or_find_association(name, parent = @base, klass = nil)
def find_association(name, parent = @base, klass = nil)
@join_dependency.instance_variable_get(:@join_root).children.detect do |assoc|
assoc.reflection.name == name &&
(@associations_pot.empty? || @associations_pot[assoc] == parent) &&
(@associations_pot.empty? || !@associations_pot.key?(assoc) || @associations_pot[assoc] == parent) &&
(!klass || assoc.reflection.klass == klass)
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,16 @@ def self.simple_escaping?
end
end

it 'sort with different joins variant' do
arr = [
Comment.create(article: Article.create(title: 'Avenger'), person: Person.create(salary: 100_000)),
Comment.create(article: Article.create(title: 'Avenge'), person: Person.create(salary: 50_000)),
]
expect(Comment.ransack(article_title_cont: 'aven',s: 'person_salary desc').result).to eq(arr)
expect(Comment.joins(:person).ransack(s: 'person_salary desc', article_title_cont: 'aven').result).to eq(arr)
expect(Comment.joins(:person).ransack(article_title_cont: 'aven',s: 'person_salary desc').result).to eq(arr)
end

it 'allows sort by `only_sort` field' do
s = Person.ransack(
's' => { '0' => { 'dir' => 'asc', 'name' => 'only_sort' } }
Expand Down