问题描述
我有两个模型,它们之间没有关联。所以,我生成了一个迁移并添加了一个关联。
class Post < ActiveRecord::Base
has_and_belongs_to_many :workspaces
end
class Workspace < ActiveRecord::Base
has_and_belongs_to_many :posts
end
class CreateJoinTablePostsUsers < ActiveRecord::Migration
def change
create_join_table :posts,:workspaces do |t|
# t.index [:post_id,:workspace_id]
# t.index [:workspace_id,:post_id]
end
end
end
我目前有一个页面,其中显示了所有帖子。但是,我在创建后的表单中添加了一个多选,以便在创建时选择一个或多个工作区。我希望能够仅显示为该特定工作区创建的帖子,而不是所有帖子,就像目前一样。 我的控制器如下:
class PostsController < Admin::BaseControlle
def index
respond_to do |format|
format.html
format.json do
@posts = Post.all.order(:order)
render json: present_collection(@posts,PostPresenter).map(&:as_json)
end
end
end
private
def post_params
params.require(:post).permit(:label,:url,:active,workspace_ids: [])
end
end
我可以通过这种方式获得关联的工作区:
Post.first.workspaces
Post.all.workspaces
我怎样才能改变我的控制器并实现它?预先感谢您的帮助!
解决方法
好吧,您应该有一个 table
,按照 Rails 惯例,您应该可以这样做:
PostsWorkspaces
以上将返回至少有一个 posts_in_workspaces = PostsWorkspaces.all.pluck(:post_id)
Posts.where(id: posts_in_workspaces )
关联的 posts
,方法 workspace
的问题是并非所有帖子都需要有一个工作区(或多个) 关联,您也可以像 Post.all.workspaces
一样考虑 Post.all
,这不是您想要完成的。
希望以上有帮助! ?
,你的想法是错误的。 Post.first.workspaces
有效,因为关联应用于返回的 Post
实例。但是 Post.all
返回一个集合。
最好的办法是执行以下操作。
# Return all posts that have a workspace associated
posts = Post.joins(:workspaces).all.distinct
posts.each do |post|
post.workspaces
end
如果您想在没有工作区的情况下包含帖子
posts = Post.includes(:workspaces).all
posts.each do |post|
post.workspaces
end