I got some problems here, I can’t make my find_by_sql request to render an ActiveRecord relation. Indeed, I need an activerecord relation to make a new request:
@searches = @searches.find_by_sql('SELECT *, COUNT( follower_id ) FROM follows GROUP BY followable_id LIMIT 0 , 3') if params[:only_famous_projects]
@project_pages = @project_pages.where(:project_id => @searches.pluck(:'followable.id')) if params[:only_famous_projects]
I can’t use “pluck” without an activerecord relation. Therefore, I think I have to convert my sql request to an Activerecord request. However, as soon as I use “count” on ActiveRecord, I have an huge problem: I don’t have on the end an ActiveRecord relation, but a FixNum!
I don’t know where to find the answer anymore, I would be really gratefull if you could help me.
Thanks
find_by_sqlwill return an ActiveRecord object only if you call it withYourModel.find_by_sql.Why not use the ActiveRecord query interface. It does a good job with calculations.
UPDATED
Notice that it will give you a Hash containing the count for each
followable_id.Isn’t
LIMIT 0, 3equivalent toLIMIT 3?