简体   繁体   English

Has_and_belong_to_many 关联

[英]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 约定称为PostsWorkspacestable ,因此您应该能够执行以下操作:

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.以上将返回至少关联一个workspacepostsPost.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.

相关问题 在种子中添加对象以关联has_and_belong_to_many? - Add objects in seeds for association has_and_belong_to_many? 具有相同模型的has_and_belong_to_many关系 - Has_and_belong_to_many relation with the same model 红宝石条件验证has_and_belong_to_many - ruby conditional validation has_and_belong_to_many 初学者-怀疑has_and_belong_to_many关联为何仅以一种方式起作用 - BEGINNER— Doubts on why a has_and_belong_to_many association is only working one way Ruby on Rails,创建Has_and_belong_to_many对象 - Ruby on Rails, creating Has_and_belong_to_many object 如何用一个 class 设置 2 个别名 has_and_belong_to_many 关系? - How to setup 2 aliased has_and_belong_to_many relationships with one class? 保存在具有has_and_belong_to_many关系的对象上时,找不到ID = 0的课程 - Couldn't find Lesson with id=0 upon save on an object with has_and_belong_to_many relationship 在Rails 3中检查“管理员”组是否在用户组中的最佳方法是什么(使用has_and_belong_to_many关系)? - What is the best way to check if the “Admin” Group is in the user groups (using has_and_belong_to_many relationships) in Rails 3? 有并属于许多Rails 4 - Has and belong to many Rails 4 如何创建和保存具有has_many / belong_to关联的模型 - How to create and save an model that has a has_many/ belong_to association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM