Question

I was advised to create a new question based on my previous one so here it is :

I have on my website a list of product, each user can create his own savings based on these products. So several users can create their own savings based on the same product. So when one user create a saving I add in the database a saving with the user_id and the product_id he refers to. Now, on one page I want to display all the savings from the other users (excluding current_user) but, as many user can create a saving based on the same product I want to display these only once.If several users create a saving based on product X I want it only displayed once,thanks

The saving table has these field :

id product_id user_id price saved

What I did is what is posted on this question : Squeel request and Heroku, Postgres: Error using GROUP BY and ORDER

I cannot get it working properly on heroku (postgres). If anyone can help me writing the query for Rails or for Squeel that would be awesome thanks !

Was it helpful?

Solution

Well Craig thanks for your help I ended up doing that and it's working on PG in local !

Saving.select("DISTINCT ON (savings.product_id) * ").where{product_id.not_in(current_user_savings.select{product_id})}.group("savings.user_id, savings.updated_at, savings.id, savings.product_id, savings.price,savings.wishlist_id, savings.saved, savings.created_at")

Having that issue made me switch my db from mysql to pg in dev so no more surprise in heroku !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top