Darius
Darius

Reputation: 524

ActiveRecord error on Heroku

I have following code working on a development environment (SQLite) but generating an error on Heroku (PostgreSQL):

@shoes_with_images = Shoe.
select( "shoes.id, shoes.collection_id, count( shoe_images.shoe_id ) as c" ).
joins( :shoe_images ).
group( "shoes.id" ).
having( "c > 0" ).
where( :collection_id => params[:id] ).
offset( (page - 1) * SHOES_PER_PAGE ).
limit( SHOES_PER_PAGE ).all

Error message:

ActiveRecord::StatementInvalid (PGError: ERROR:  column "c" does not exist

How can I correct this query to make it work on Postgres?

Upvotes: 0

Views: 81

Answers (1)

Augusto
Augusto

Reputation: 30007

Try changing

having( "c > 0" )

with

having( "count( shoe_images.shoe_id ) > 0" )

I'm not 100% sure, but I think that the query processor doesn't have the alias "c" when it processes the group by or having statements.

Upvotes: 1

Related Questions