Created
August 25, 2012 21:29
-
-
Save wheeyls/3471151 to your computer and use it in GitHub Desktop.
This file contains 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
category.posts | |
#=> [] # list of all posts | |
posts.categories | |
#=> [] # list of all categories |
This file contains 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
class Post | |
has_and_belongs_to_many :categories | |
scope :in_categories, lambda do |cat_ids| | |
joins(:categories) | |
.where(id: cat_ids) | |
.uniq | |
end | |
end |
This file contains 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
class Category | |
has_and_belongs_to_many :posts | |
end | |
class Post | |
has_and_belongs_to_many :categories | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment