0

My controller has the following query :

# reports_between_two_dates is already defined
@data_count_by_city = reports_between_two_dates
                        .joins('JOIN "places" ON "places"."id"="reported_regs"."reported_place_id" JOIN "cities" ON "places"."city_id" = "cities"."id" ')
                        .where('"reported_regs"."reported_place_id" IN (SELECT "places"."id" FROM "places") AND "places"."city_id" IN (SELECT "cities"."id" FROM "cities")')
                        .group('"display_name"')
                        .select('COUNT(*) AS count, "display_name" AS cities')
                        .order('count DESC')
                        .limit(3)

I also have this in the controller :

respond_to do |format|
  format.js { 
    render json: { 
    html: render_to_string(
      partial: 'data_stats', 
      locals: { 
        data_count_by_city: @data_count_by_city
    }) } }
  format.html
end

The query's result in psql is as follows :

 count |    cities     
-------+---------------
   409 | NYC
   244 | SF
   156 | LA

My end goal is to display the above result as a hash, having as keys the cities, and the count as values..

In the view I've tried doing :

<td><%= Hash[@data_count_by_city.map{ |r| [r.cities,r.count] }] %></td>

But all I get is an empty hash.. What am I missing?

9
  • What's the query result in Ruby: @data_count_by_city.inspect? Commented Sep 8, 2017 at 15:00
  • so, you want to render a hash in erb? Commented Sep 8, 2017 at 15:02
  • @Leito the result is : #<ActiveRecord::Relation []> Commented Sep 8, 2017 at 15:06
  • 2
    There are no results, and that's why you get an empty hash. So let's go back to the query and forget about the controller and view. What's the query in SQL? What's the query you see in the logs of your Rails Server? Commented Sep 8, 2017 at 15:08
  • 1
    You should not need a custom join string if your associations are properly defined. Can you include the models? Commented Sep 8, 2017 at 15:33

1 Answer 1

1

It appears the relation was returning no results (empty relation: #<ActiveRecord::Relation []>). OP find out he was running the query on Production and the code on a different environment.

Other than that, the code seems to work as expected, as long as @data_count_by_city stores an array-like structure with both cities and count. On that note, I think

.group('display_name').count shouldn't need the .select('COUNT(*) AS count, "display_name" AS cities')

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Leito for the heads up :) Btw if I select certain dates I get an empty hash I assume it's because of the Inner Join, I tried doing the Outer Join but I still get no results. Any ideas?
Probably, but same advice goes: compare the queries in your server logs to the ones in psql. Then slowly deconstruct the query to see what causes it to be empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.