简体   繁体   中英

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.

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/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

2- The assignment is given to multiple users. Users are associated to assignments using 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)

This works for user:

assignment.rb

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

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.

I have a designer dashboard where i only want to display assignments given to the current user.

designer_dashboard controller:

#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. A Task has_many Designers through AssignedTasks. The association between Task and User can be named as 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.

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.

Also, I seem to remember reading somewhere that Task is a reserved word. You may want to rename task to something else.

Margo answer is correct.

This works in controller:

@assignments = current_user.assignments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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