Skip to content Skip to sidebar Skip to footer

Find_by_sql Renders An Array

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 = @search

Solution 1:

find_by_sql will return an ActiveRecord object only if you call it with YourModel.find_by_sql.

Why not use the ActiveRecord query interface. It does a good job with calculations.

UPDATED

@searches = @searches.group(:followable_id).limit(3).offset(0).count(:follower_id) if params[:only_famous_projects]

Notice that it will give you a Hash containing the count for each followable_id.

Isn't LIMIT 0, 3 equivalent to LIMIT 3 ?

Solution 2:

COUNT will always return a FixNUM, because you asked the database to count the number of rows.

You should really try to use find_by_sql as a last resort as it is only meant to bypass ActiveRecord for things that it can not do. And even for things that ActiveRecord doesn't support, you can always see if you can use the Squeel or Valium gems to handle edge-cases.

Another reason not to use find_by_sql is that, for example, using MySQL specific terms will lock you out of using other databases in the future.

Post a Comment for "Find_by_sql Renders An Array"