Skip to content

Commit 69612c8

Browse files
Copilotscarroll32
andauthored
Fix NoMethodError in type_for when column doesn't exist in ransackable_attributes (#1616)
* Initial plan * Fix NoMethodError in type_for method when column doesn't exist Co-authored-by: scarroll32 <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: scarroll32 <[email protected]>
1 parent 340b563 commit 69612c8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/ransack/adapters/active_record/context.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def type_for(attr)
1919
unless schema_cache.send(:data_source_exists?, table)
2020
raise "No table named #{table} exists."
2121
end
22-
attr.klass.columns.find { |column| column.name == name }.type
22+
column = attr.klass.columns.find { |column| column.name == name }
23+
column&.type
2324
end
2425

2526
def evaluate(search, opts = {})

spec/ransack/adapters/active_record/context_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ module ActiveRecord
140140
expect(attribute.relation.name).to eq 'articles'
141141
expect(attribute.relation.table_alias).to be_nil
142142
end
143+
144+
describe '#type_for' do
145+
it 'returns nil when column does not exist instead of raising NoMethodError' do
146+
# Create a mock attribute that references a non-existent column
147+
mock_attr = double('attribute')
148+
allow(mock_attr).to receive(:valid?).and_return(true)
149+
150+
mock_arel_attr = double('arel_attribute')
151+
allow(mock_arel_attr).to receive(:relation).and_return(Person.arel_table)
152+
allow(mock_arel_attr).to receive(:name).and_return('nonexistent_column')
153+
allow(mock_attr).to receive(:arel_attribute).and_return(mock_arel_attr)
154+
allow(mock_attr).to receive(:klass).and_return(Person)
155+
156+
# This should return nil instead of raising an error
157+
expect(subject.type_for(mock_attr)).to be_nil
158+
end
159+
end
143160
end
144161
end
145162
end

0 commit comments

Comments
 (0)