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

arel 2.2.3 take() becomes -1 when skip() is used #136

Closed
sunaku opened this issue Sep 14, 2012 · 4 comments
Closed

arel 2.2.3 take() becomes -1 when skip() is used #136

sunaku opened this issue Sep 14, 2012 · 4 comments

Comments

@sunaku
Copy link

sunaku commented Sep 14, 2012

Hello,

In arel 2.2.3, I observe that take(n) is being forced into LIMIT -1 (regardless of n value) when skip() is also used:

Loading development environment (Rails 3.1.3)
## ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
>> Arel::Table.new(:foobar).take(3).skip(3).to_sql
"SELECT  FROM \"foobar\"  LIMIT -1 OFFSET 3"
>> Arel::Table.new(:foobar).take(2).skip(3).to_sql
"SELECT  FROM \"foobar\"  LIMIT -1 OFFSET 3"
>> Arel::Table.new(:foobar).take(1).skip(3).to_sql
"SELECT  FROM \"foobar\"  LIMIT -1 OFFSET 3"
>> Arel::Table.new(:foobar).take(1).to_sql
"SELECT  FROM \"foobar\"  LIMIT 1"
>> Arel::Table.new(:foobar).take(2).to_sql
"SELECT  FROM \"foobar\"  LIMIT 2"
>> Arel::Table.new(:foobar).take(3).to_sql
"SELECT  FROM \"foobar\"  LIMIT 3"

I expect take(n) to become LIMITn in the generated SQL query.

Thanks for your consideration.

@sunaku
Copy link
Author

sunaku commented Sep 14, 2012

The Arel::Nodes::SelectStatement class seems relevant to this issue:

>> ast = Arel::Table.new(:foobar).take(2).skip(3).ast; ast.cores.clear; ast
#<Arel::Nodes::SelectStatement:0x0000000496c1d0
 @cores=[],
 @limit=#<Arel::Nodes::Limit:0x0000000496c068 @expr=2>,
 @lock=nil,
 @offset=#<Arel::Nodes::Offset:0x0000000496c018 @expr=3>,
 @orders=[],
 @with=nil>
>> ast.to_sql
" LIMIT -1 OFFSET 3"

Hope that helps.

@sunaku
Copy link
Author

sunaku commented Sep 18, 2012

I have upgraded to Arel 3.0.2 and this issue no longer occurs. :-)

@sunaku sunaku closed this as completed Sep 18, 2012
@sunaku
Copy link
Author

sunaku commented Sep 20, 2012

I found the actual problem in this monkey patch from my own code:

class Arel::Nodes::Node
  # allow boolean compositions
  alias & and
  alias | or
  alias ! not # <== causes the "LIMIT -1" bug!
end

Solved it by using ~ instead of ! as an alias for not. Cheers.

@ernie
Copy link
Collaborator

ernie commented Sep 20, 2012

Thanks for following up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants