Relation count returns syntax error in Rails 4.1.X

48 views
Skip to first unread message

Rodrigo Lueneberg

unread,
May 31, 2014, 8:00:36 PM5/31/14
to rubyonra...@googlegroups.com
Until Rails 4.0.4 everything was working fine until I upgraded to latest
Rails version. It seems to be a bug because from the error message looks
like rails is doing just a count(Bla bla )without using properly using a
select count(id) from (SELECT COUNT(orders_header.id,
orders_header.created_at) FROM `orders_header` WHERE (shop_id=99 and
customer_id=1 and hash_key like
'539de64e8793790430052bc861dd0ff521334e32')

Is there a workaround for this problem?

query= OrderHeader.select("orders_header.id,
orders_header.created_at").where("shop_id=#{shop_id} and
customer_id=#{customer_id} and hash_key like
'#{current_hash_key}'").order("id desc")
if query.nil?
return true # no duplicates found
end
if (query.count>0) # duplicates found
# I get the error righ here
end
ERROR
SELECT COUNT(orders_header.id, orders_header.created_at) FROM
`orders_header` WHERE (shop_id=99 and customer_id=1 and hash_key like
'539de64e8793790430052bc861dd0ff521334e32')
Mysql2::Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near ' orders_header.created_at) FROM `orders_header` WHERE
(shop_id=99 and customer_' at line 1: SELECT COUNT(orders_header.id,
orders_header.created_at) FROM `orders_header` WHERE (shop_id=99 and
customer_id=1 and hash_key like
'539de64e8793790430052bc861dd0ff521334e32')

--
Posted via http://www.ruby-forum.com/.

Josh Jordan

unread,
Jun 1, 2014, 8:02:58 AM6/1/14
to rubyonra...@googlegroups.com
Rails introduced a breaking change to select and count being used together in the same relation. Either remove your call to "select", since it is unnecessary here, or call "count(:all)".

Rodrigo Lueneberg

unread,
Jun 1, 2014, 9:20:44 AM6/1/14
to rubyonra...@googlegroups.com
Josh,

If I remove the call to "select" how to specify which columns to select?
PS: You don't want Rails to select all using wildcard * as it will
affect
performance.

Thanks
Rod

josh....@gmail.com

unread,
Jun 1, 2014, 9:47:38 AM6/1/14
to rubyonra...@googlegroups.com
If you're calling count, it doesn't matter what select values you've passed, since you're asking ActiveRecord to return an aggregate, not any column values.


--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/b3YH5shQnsY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f0ef7d96568fed8082a461502ddb4429%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Rodrigo Lueneberg

unread,
Jun 1, 2014, 10:53:34 AM6/1/14
to rubyonra...@googlegroups.com
I see that, but sometimes you want to reuse code in the same relation.
For instance if you do a query first using restricted columns and later
you need to do a count based on the result. Let's say in the first
relation with selected columns (col1, col2, etc.) you want to use to
fill an array. From what I understand it doesn't work if I try to obtain
a count from this relation. In fact, it doesn't make sense to do a
relation just to do a count.

In this scenario you would need two relations, one for the selected
columns and another only for count. I would prefer do it using raw SQL,
don't you agree?

Rod

josh....@gmail.com wrote in post #1148444:
> If you're calling count, it doesn't matter what select values you've
> passed, since you're asking ActiveRecord to return an aggregate, not any
> column values.
>
>
> On Sun, Jun 1, 2014 at 9:19 AM, Rodrigo Lueneberg <li...@ruby-forum.com>

josh....@gmail.com

unread,
Jun 1, 2014, 10:55:14 AM6/1/14
to rubyonra...@googlegroups.com

That was the point I was making in the issue I opened, yes.

--
You received this message because you are subscribed to a topic in the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rubyonrails-talk/b3YH5shQnsY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Colin Law

unread,
Jun 1, 2014, 11:00:15 AM6/1/14
to rubyonra...@googlegroups.com
On 1 June 2014 15:51, Rodrigo Lueneberg <li...@ruby-forum.com> wrote:
> I see that, but sometimes you want to reuse code in the same relation.
> For instance if you do a query first using restricted columns and later
> you need to do a count based on the result. Let's say in the first
> relation with selected columns (col1, col2, etc.) you want to use to
> fill an array. From what I understand it doesn't work if I try to obtain
> a count from this relation. In fact, it doesn't make sense to do a
> relation just to do a count.
>
> In this scenario you would need two relations, one for the selected
> columns and another only for count. I would prefer do it using raw SQL,
> don't you agree?

Why not just do the count on the array? That would save running two
queries, one for the data and one for the count. Or am I missing
something?

Colin

>
> Rod
>
> josh....@gmail.com wrote in post #1148444:
>> If you're calling count, it doesn't matter what select values you've
>> passed, since you're asking ActiveRecord to return an aggregate, not any
>> column values.
>>
>>
>> On Sun, Jun 1, 2014 at 9:19 AM, Rodrigo Lueneberg <li...@ruby-forum.com>
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f824781ea8e576e1eec1c6341083e02d%40ruby-forum.com.

Rodrigo Lueneberg

unread,
Jun 1, 2014, 11:03:20 AM6/1/14
to rubyonra...@googlegroups.com
Thanks, I am glad we are on the same page. Sorry, I did not see your
github post. I will check that right now.
Rod

Rodrigo Lueneberg

unread,
Jun 1, 2014, 11:48:18 AM6/1/14
to rubyonra...@googlegroups.com
By the way what would be more efficient, count(:all) or count(1)?
thanks
Reply all
Reply to author
Forward
0 new messages