简体   繁体   English

Rails 4返回与多个用户相关的任务

[英]Rails 4 return tasks associated with more than one user

Question: How can I return Assignments associates with one of many users in the users array? 问题:如何返回与用户数组中许多用户之一相关联的工作分配?

I researched the Rails guides and some only posts but I can't figure this out yet. 我研究了Rails指南和仅一些帖子,但是我还不能弄清楚。

https://codereview.stackexchange.com/questions/46319/is-there-a-better-approach-to-searching-has-and-belongs-to-many-relations https://codereview.stackexchange.com/questions/46319/is-there-a-better-approach-to-searching-has-and-belongs-to-many-relations

http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association

http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects-in-batches http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects-in-batches

I am associating users to assignments two different ways. 我将用户分配给两种不同的方式。

1- user "user_id" is the one who creates the assignment 1个用户“ user_id”是创建分配的用户

2- The assignment is given to multiple users. 2-分配已分配给多个用户。 Users are associated to assignments using has_and_belongs_to_many :users 使用has_and_belongs_to_many将用户与分配相关联:users

Basically each assignment is associated to the user who owns the task. 基本上,每个分配都与拥有任务的用户相关联。 The assignment is also given to multiple users who will work on it. 分配还会分配给将要使用它的多个用户。

I can successfully return all tasks associated with user but not with users. 可以成功返回与用户关联的所有任务,但不能返回与用户关联的所有任务。 I'm trying to display only the tasks associated with the current_user (devise) 我正在尝试仅显示与current_user相关联的任务(设计)

This works for user: 这适用于用户:

assignment.rb Assignment.rb

class Assignment < ActiveRecord::Base
  belongs_to :deliverable
  belongs_to :user
  has_and_belongs_to_many :users
end

user.rb user.rb

  has_and_belongs_to_many :assignments

The association are working fine. 该协会运作良好。 In the console I can get all the users associated via HABTM to the assignment. 在控制台中,我可以使所有通过HABTM关联到分配的用户。

I have a designer dashboard where i only want to display assignments given to the current user. 我有一个设计师仪表板,我只想显示分配给当前用户的任务。

designer_dashboard controller: designer_dashboard控制器:

#if I do this I'll get all the assignments:
@assignments = Assignment.all
# but I want to be able to do something like this to get only the assingments associated with the current user via HABTM
@assignments = Assignment.includes(:users).where(["user_ids =?", current_user])

The data isn't modelled to reflect the 2 different types of relationship that exist between users and tasks - task owners and task designers. 数据的建模不能反映用户和任务之间存在的两种不同类型的关系-任务所有者和任务设计者。 You've only set up one of them. 您只设置了其中之一。

You will need to remodel the data and give the relationships more meaningful names. 您将需要重塑数据并为关系指定更有意义的名称。

One way would be to use has_many_through for the association that a Task has with who its assigned to. 一种方法是将has_many_through用于任务与任务分配给谁的关联。 A Task has_many Designers through AssignedTasks. 任务通过AssignedTasks具有多个设计器。 The association between Task and User can be named as TaskOwner. Task和User之间的关联可以命名为TaskOwner。 If you set up both these associations you will be able to get current_user.owned tasks and current_user.assigned tasks which is more clear than referring to user and users. 如果同时设置了这两个关联,则将能够获得current_user.owned任务和current_user.assigned任务,这比引用用户和用户更为清楚。

class Task
  belongs_to :task_owner, class_name: "User"
  has_many :assigned_tasks
  has_many :designers, through: assigned_tasks, class_name: "User"
end

class User
  has_many :owned_tasks, class_name: "Task"
  has_many :assigned_tasks, foreign_key: designer_id
  has_many :tasks, through: :assigned_tasks, 
end

class AssignedTask
  belongs_to :designer
  belongs_to :task
end

You will need to generate some migrations to add the requisite ids. 您将需要生成一些迁移以添加必需的ID。

Also, I seem to remember reading somewhere that Task is a reserved word. 另外,我似乎还记得在某处读到Task是保留字。 You may want to rename task to something else. 您可能需要将任务重命名为其他名称。

Margo answer is correct. 马戈的答案是正确的。

This works in controller: 这适用于控制器:

@assignments = current_user.assignments @assignments = current_user.assignments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM