user984621
user984621

Reputation: 48443

Rails - how to make join between these tables?

I have these tables:

users - contains list of all users in the system

cars - list of all cars, there is a column user_id

car_views - for tracking how many people visit profile of a car. There are following columns:

- car_id
- ip

I am trying to fetch the information, how many people saw cars of a respective user within a time period. But my problem is, that I am not sure how can I fetch all views of cars from a respective user.

There would be a solution to put to the car_views table the column user_id, but it would be a duplication of information, or not?

Upvotes: 1

Views: 62

Answers (1)

Ismael
Ismael

Reputation: 16720

You should be able to do something like this

user.cars.joins(:car_views).group('car_views.car_id').select('count(car_views.id) as count, car.name')

I'm not sure if this will work but you can give it a try

Upvotes: 1

Related Questions