Skip to content

Commit

Permalink
Merge pull request #800 from mataki/fix-join-not-null
Browse files Browse the repository at this point in the history
Fix query of not_null with joining association
  • Loading branch information
scarroll32 authored Jun 12, 2017
2 parents f749d08 + bfebca2 commit 0d8db3b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/ransack/adapters/active_record/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ def arel_predicate
association = attribute.parent
if negative? && attribute.associated_collection?
query = context.build_correlated_subquery(association)
query.where(format_predicate(attribute).not)
context.remove_association(association)
Arel::Nodes::NotIn.new(context.primary_key, Arel.sql(query.to_sql))
if self.predicate_name == 'not_null' && self.value
query.where(format_predicate(attribute))
Arel::Nodes::In.new(context.primary_key, Arel.sql(query.to_sql))
else
query.where(format_predicate(attribute).not)
Arel::Nodes::NotIn.new(context.primary_key, Arel.sql(query.to_sql))
end
else
format_predicate(attribute)
end
Expand Down
22 changes: 22 additions & 0 deletions spec/ransack/predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,28 @@ module Ransack
field = "#{quote_table_name("people")}.#{quote_column_name("name")}"
expect(@s.result.to_sql).to match /#{field} IS NULL/
end

describe 'with association qeury' do
it 'generates a value IS NOT NULL query' do
@s.comments_id_not_null = true
sql = @s.result.to_sql
parent_field = "#{quote_table_name("people")}.#{quote_column_name("id")}"
expect(sql).to match /#{parent_field} IN/
field = "#{quote_table_name("comments")}.#{quote_column_name("id")}"
expect(sql).to match /#{field} IS NOT NULL/
expect(sql).not_to match /AND NOT/
end

it 'generates a value IS NULL query when assigned false' do
@s.comments_id_not_null = false
sql = @s.result.to_sql
parent_field = "#{quote_table_name("people")}.#{quote_column_name("id")}"
expect(sql).to match /#{parent_field} NOT IN/
field = "#{quote_table_name("comments")}.#{quote_column_name("id")}"
expect(sql).to match /#{field} IS NULL/
expect(sql).to match /AND NOT/
end
end
end

describe 'present' do
Expand Down

0 comments on commit 0d8db3b

Please sign in to comment.