Rails 4.1 activerecord_relation conversion to array problem

108 views
Skip to first unread message

Rodrigo Lueneberg

unread,
Apr 10, 2014, 9:34:12 PM4/10/14
to rubyonra...@googlegroups.com
After I upgraded from rails 3.2 to 4.1 I am getting this error
below.

I researched that Rails 4.1 requires a conversion of an
activerecord_relation to an array using to_a. I did use to_a but looks
like rails is doing a select count behind the scenes. Do you know how to
fix this?


def index
order_type = params[:order_type]
@oh = OrderHeader.select("orders_header.*, users.username, users.id
as customer_id, users.email, users.phone_work, users.phone_home,
users.phone_cell").
joins("INNER JOIN users ON users.id = orders_header.customer_id
").
where("orders_header.order_type='#{order_type}' and
orders_header.customer_id=#{session[:user_id]}").
order("id desc").
paginate(:per_page => 10, :page => params[:page])
return @oh.to_a
end

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 '*, users.username, users.id as customer_id, users.email,
users.phone_work, users' at line 1: SELECT COUNT(orders_header.*,
users.username, users.id as customer_id, users.email, users.phone_work,
users.phone_home, users.phone_cell) FROM `orders_header` INNER JOIN
users ON users.id = orders_header.customer_id WHERE
(orders_header.order_type='order' and orders_header.customer_id=1)

thanks
Rod

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

Frederick Cheung

unread,
Apr 12, 2014, 7:50:19 AM4/12/14
to rubyonra...@googlegroups.com


On Friday, April 11, 2014 2:34:12 AM UTC+1, Ruby-Forum.com User wrote:
After I upgraded from rails 3.2 to 4.1 I am getting this error
below.

I researched that Rails 4.1 requires a conversion of an
activerecord_relation to an array using to_a. I did use to_a but looks
like rails is doing a select count behind the scenes. Do you know how to
fix this?

I don't think the to_a stuff is relevant. Your pagination library (will_paginate?) is counting the total number of entries and appears to be producing a bad query to do that, because of the select query. Perhaps try updating it?

Fred 

Rodrigo Lueneberg

unread,
Apr 17, 2014, 8:22:38 AM4/17/14
to rubyonra...@googlegroups.com
Thanks, I did updated will_paginate gem and still the problem. Looks
like it is not compatible with rails 4.1

The last version is of 3.0.5 September 18, 2013 (34 KB) according to the
link below:
https://rubygems.org/gems/will_paginate/versionsOn

However, on rails 4.0.4 it works beautifully.

Rod

> On Friday, April 11, 2014 2:34:12 AM UTC+1, Ruby-Forum.com User wrote:
>>
>> After I upgraded from rails 3.2 to 4.1 I am getting this error
>> below.
>>
>> I researched that Rails 4.1 requires a conversion of an
>> activerecord_relation to an array using to_a. I did use to_a but looks
>> like rails is doing a select count behind the scenes. Do you know how to
>> fix this?
>>
>> I don't think the to_a stuff is relevant. Your pagination library
> (will_paginate?) is counting the total number of entries and appears to
> be
> producing a bad query to do that, because of the select query. Perhaps
> try
> updating it?
>
> Fred

Walter Lee Davis

unread,
Apr 17, 2014, 9:21:42 AM4/17/14
to rubyonra...@googlegroups.com
I believe Kaminari can be dropped in (uses the same verbs) and it might work for you. (Last commit was 7 days ago, if that means anything.)

Walter
> --
> 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/7b60d247d12663e6a20c0b0c4decd4c9%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Rodrigo Lueneberg

unread,
Apr 17, 2014, 9:40:38 AM4/17/14
to rubyonra...@googlegroups.com
Thanks Walter, I did not know about this gem.

Walter Davis wrote in post #1143372:
> I believe Kaminari can be dropped in (uses the same verbs) and it might
> work for you. (Last commit was 7 days ago, if that means anything.)
>
> Walter

Reply all
Reply to author
Forward
0 new messages