Ser*_*gey 7 ruby model ruby-on-rails associations
我想列出与某个特定类别和课堂相关的所有帖子.
我有:
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
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情
Post.where(["category.id = ? AND classroom.id = ?", params[:category_id], params[:classroom_id]])
Run Code Online (Sandbox Code Playgroud)
这确实是非常简单的任务,但我不知道我应该寻找什么(关键字).
这就像同样的问题,这个,但是在轨道上.
编辑:我在问题中添加了更多细节.这有效,但前提是我指定了两个参数.女巫并非总是如此 - 我不知道会指定什么样的参数.
Post.joins(:categories, :classrooms).where(["categories.id = ? AND classrooms.id = ?", params[:classroom_id], params[:category_id]])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26161 次 |
| 最近记录: |