[英]Has_and_belong_to_many association
I have two models with no associations between them.我有两个模型,它们之间没有关联。 So, I generated a migration and added an association.
因此,我生成了一个迁移并添加了一个关联。
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
I currently have a page where all the posts are shown.我目前有一个显示所有帖子的页面。 However, I added a multi select in the post creation form in order to select one workspace or more when creating.
但是,我在创建后表单中添加了一个多 select 以便在创建时 select 一个或多个工作区。 I would like to be able to show only the posts that were created for that particular workspace instead of all of them, as it is at the moment.
我希望能够仅显示为该特定工作区创建的帖子,而不是所有帖子,就像目前一样。 My controller is as follows:
我的controller如下:
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
I am able to get the associated workspace this way:我可以通过这种方式获得相关的工作区:
Post.first.workspaces Post.first.workspaces
But I would like to show all the posts and I get an error when I try this command:但是我想显示所有帖子,但在尝试此命令时出现错误:
Post.all.workspaces Post.all.workspaces
How could I change my controller and accomplish that?我怎样才能改变我的 controller 并做到这一点? Thank you in advance for your help!
预先感谢您的帮助!
Well you should have a table
called PostsWorkspaces
by rails convention so you should be able to do something like:好吧,您应该有一个按照 Rails 约定称为
PostsWorkspaces
的table
,因此您应该能够执行以下操作:
posts_in_workspaces = PostsWorkspaces.all.pluck(:post_id)
Posts.where(id: posts_in_workspaces )
The above will return the posts
that have at least one workspace
associated, the problem with the approach Post.all.workspaces
is that not all posts need to have a workspace (or more than one) associated, also you can think on Post.all
like select * from posts
which is not what you want to accomplish.以上将返回至少关联一个
workspace
的posts
, Post.all.workspaces
方法的问题在于并非所有帖子都需要关联一个工作空间(或多个),您也可以在Post.all
上思考像select * from posts
,这不是您想要完成的。
Hope the above helps!希望以上对您有所帮助!
You are thinking about this the wrong way.你正在以错误的方式思考这个问题。
Post.first.workspaces
works because the association is applied on the instance of Post
returned. Post.first.workspaces
有效,是因为该关联应用于返回的Post
实例。 But Post.all
returns a collection.但是
Post.all
返回一个集合。
Your best bet is to do something like the following.您最好的选择是执行以下操作。
# Return all posts that have a workspace associated
posts = Post.joins(:workspaces).all.distinct
posts.each do |post|
post.workspaces
end
If you want to include posts without a workspace如果您想包含没有工作区的帖子
posts = Post.includes(:workspaces).all
posts.each do |post|
post.workspaces
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.