Skip to content

Work around a bug in Rails 5, where LIMIT causes trouble

Toon Claes requested to merge tc-fix-rails5-subquery-selfjoin into master

The original code caused Rails to generate invalid SQL. The problem lays in the .arel method in ActiveRecord::Relation. When there was a limit on the relation, the LIMIT statement was taken over to Arel, but the value wasn't.

relation = Event.limit(2)
relation.to_sql
#=> "SELECT  `events`.* FROM `events` LIMIT 2"
relation.arel.to_sql
#=> "SELECT  `events`.* FROM `events` LIMIT ?"

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/51729

Merge request reports