I want to distinct the duplicated data with has_many through

problem

For example, if there is a model of this kind of relationship

class User < ApplicationRecord
  has_many :lists
end

class List < ApplicationRecord
  belongs_to :user
  belongs_to :product
end

class Product < ApplicationRecord
  has_many :lists
end

User has multiple Lists, each List references Product ... I want to bring all the products related to User ...

Solutions

class User < ApplicationRecord
  has_many :lists

  #If this is the case, duplicate Products will be returned for the number of Lists.
  # has_many :products, through: :lists 

  #I'll insert a distinct
  has_many :products, ->{ distinct }, through: :lists
end

Very convenient (see [here] 1)

Recommended Posts

I want to distinct the duplicated data with has_many through
[Java] I want to perform distinct with the key in the object
I want to dark mode with the SWT app
I want to pass the startup command to postgres with docker-compose.
I want to fetch another association of the parent model from the intermediate table with has_many
I want to return to the previous screen with kotlin and java!
[Rails] I want to add data to Params when transitioning with link_to
I want to use DBViewer with Eclipse 2018-12! !!
I want to introduce the committee with Rails without getting too dirty
I want to output the day of the week
[For beginners] I want to automatically enter pre-registered data in the input form with a selection command.
[NCMB] I searched the data store with mbaas
I want to use java8 forEach with index
I want to var_dump the contents of the intent
I want to play with Firestore from Rails
I want to truncate after the decimal point
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to get the value in Ruby
[Eclipse] I want to use the completion function, but I want to manage to confirm the completion with spaces.
I want to get only the time from Time type data ...! [Strftime] * Additional notes
[Java] I want to calculate the difference from the date
I want to judge the range using the monthly degree
I tried to get started with Spring Data JPA
I want to display the name of the poster of the comment
I want to monitor a specific file with WatchService
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method
I want to get along with Map [Java beginner]
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to redirect sound from Ubuntu with xrdp
I want to simplify the log output on Android
I want to add a delete function to the comment function
I want to change the path after new registration after logging in with multiple devises.
I want to hit the API with Rails on multiple docker-composes set up locally
[Beginner] I want to modify the migration file-How to use rollback-
I want to set the conditions to be displayed in collection_check_boxes
I tried to increase the processing speed with spiritual engineering
[Rails] [bootstrap] I want to change the font size responsively
I want to use screen sharing on the login screen on Ubuntu 18
I want to convert characters ...
(ยด-`) .. oO (I want to easily find the standard output "Hello".
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.
I want to push an app made with Rails 6 to GitHub
I want to bring Tomcat to the server and start the application
I want to expand the clickable part of the link_to method
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
I want to use the Java 8 DateTime API slowly (now)
I want to create a form to select the [Rails] category
I want to use the sanitize method other than View.
What I was addicted to with the Redmine REST API
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to put the JDK on my Mac PC
I want to give a class name to the select attribute
I want to manually send an authorization email with Devise
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
I want to recursively search the class list under the package
I want to return a type different from the input element with Java8 StreamAPI reduce ()