Skip to content

Instantly share code, notes, and snippets.

@wheeyls
Created August 25, 2012 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wheeyls/3471151 to your computer and use it in GitHub Desktop.
Save wheeyls/3471151 to your computer and use it in GitHub Desktop.
category.posts
#=> [] # list of all posts
posts.categories
#=> [] # list of all categories
class Post
has_and_belongs_to_many :categories
scope :in_categories, lambda do |cat_ids|
joins(:categories)
.where(id: cat_ids)
.uniq
end
end
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