Question

In my controller, I get movies with:

movies = Movie.active.includes(:likes).references(:likes).where("likes.user_id = ?", user.id).joins(:collections).active.uniq

I then order and filter them:

@movies = movies.order(query_order).offset(offset).limit(limit + 1)

This output in an PG:Error: PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

I quickly found at that you have to select the fields you order by. So I added .select("movies.*, likes.*") to the query.

movies = Movie.select("movies.*, likes.*").active.includes(:likes).references(:likes).where("likes.user_id = ?", user.id).joins(:collections).active.uniq

It works fine except now Paperclip messes up big time. The HTML gets rendered exactly the same as before, but somehow the image doesn't show/process.

=image_tag movie.cover.url(:expanded), :class => "expanded"

When I remove .select("movies.*, likes.*") and .uniq from the query it has duplicate items, but the images work fine.

Was it helpful?

Solution

Because I joined movies with likes it both retrieved the movie.id and the likes.id.

This this paperclip helper couldn't cope with two id's so it randomly picked the likes id.

=image_tag movie.cover.url(:expanded), :class => "expanded" 

As a result wrong paths got created: iE. /movies/system/007/movie_with_id_01_image.jpg.

I had to change my select clause, now excluding the likes.id.

.select("movies.*, likes.*")
.select("movies.*, likes.column_a, likes.column_b, ... , likes.column_x")

Now it finally worked again.

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