I would like to list all posts related to a specific category and class.
I have:
class Post < ActiveRecord::Base has_many :category_posts has_many :categories, :through => :category_posts has_many :classroom_posts has_many :classrooms, :through => :classroom_posts end class Category < ActiveRecord::Base has_many :category_posts has_many :posts, :through => :category_posts end class CategoryPost < ActiveRecord::Base belongs_to :category belongs_to :post end class Classroom < ActiveRecord::Base has_many :classroom_posts has_many :posts, :through => :classroom_posts end class ClassroomPost < ActiveRecord::Base belongs_to :classroom belongs_to :post end
And I want to do something like this
Post.where(["category.id = ? AND classroom.id = ?", params[:category_id], params[:classroom_id]])
This is really a very simple task, but I donβt know what I need to search for (keywords).
This is the same problem as this , but in rails.
EDIT : I added additional information to the question. This works, but only if I have both options. A witch is not always like that - I do not know what parameters will be specified.
Post.joins(:categories, :classrooms).where(["categories.id = ? AND classrooms.id = ?", params[:classroom_id], params[:category_id]])
ruby ruby-on-rails associations model
Sergey
source share