|
218 | 218 | expect(Article.i18n.where(title: "Title", content: "Content")).to eq([article2]) |
219 | 219 | end |
220 | 220 | end |
| 221 | + |
| 222 | + describe "regression: select_for_count with mixed select values" do |
| 223 | + # Test for bug where select_for_count called .right on string values |
| 224 | + # causing NoMethodError when select_values contained non-Arel nodes |
| 225 | + if ::ActiveRecord::VERSION::MAJOR >= 8 |
| 226 | + before do |
| 227 | + stub_const 'Article', Class.new(ActiveRecord::Base) |
| 228 | + translates Article, :title, backend: :table |
| 229 | + end |
| 230 | + |
| 231 | + it "handles count with regular column in select" do |
| 232 | + Article.create(title: "Article 1") |
| 233 | + Article.create(title: "Article 2") |
| 234 | + |
| 235 | + # The original bug: calling .right on string values like "id" |
| 236 | + # This should not raise NoMethodError: undefined method `right' for "id":String |
| 237 | + expect { Article.select(:id).count }.not_to raise_error |
| 238 | + expect(Article.select(:id).count).to eq(2) |
| 239 | + end |
| 240 | + |
| 241 | + it "handles count with translated column in select" do |
| 242 | + Article.create(title: "Article 1") |
| 243 | + Article.create(title: "Article 2") |
| 244 | + |
| 245 | + # When selecting translated attributes, Mobility creates Arel::Nodes::As with aliases |
| 246 | + # This should properly filter out the aliased columns for counting |
| 247 | + relation = Article.i18n.select(:title) |
| 248 | + |
| 249 | + expect { relation.count }.not_to raise_error |
| 250 | + expect(relation.count).to eq(2) |
| 251 | + end |
| 252 | + |
| 253 | + it "handles count without select when mobility is present" do |
| 254 | + Article.create(title: "Article 1") |
| 255 | + Article.create(title: "Article 2") |
| 256 | + |
| 257 | + # Ensure normal counting still works on models with mobility |
| 258 | + expect { Article.count }.not_to raise_error |
| 259 | + expect(Article.count).to eq(2) |
| 260 | + |
| 261 | + expect { Article.i18n.count }.not_to raise_error |
| 262 | + expect(Article.i18n.count).to eq(2) |
| 263 | + end |
| 264 | + end |
| 265 | + end |
221 | 266 | end |
0 commit comments