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?
Try changing
with
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.