Created
January 23, 2017 10:44
-
-
Save 8parth/27400963f350000c5811a461c7303ae3 to your computer and use it in GitHub Desktop.
has_many :through association with specific conditions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # user.rb | |
| # simple | |
| has_many :posts, dependent: :destroy # all posts | |
| has_many :published_posts, -> { where(published: true) }, class_name: "Post" # all published posts | |
| # advanced | |
| has_many :comments, through: :posts, dependent: :destroy | |
| has_many :comments_on_published_posts, through: :published_posts, dependent: :destroy # comments on published posts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment