I have a projects app that allows all Users on a team to create Tasks, I'd like a User to be able to add users to a Task (for example if multiple people were in a meeting). Essentially the new task form would have all the usernames of those on the team and the user could check a box by each name for anyone else who participated in the task.
Initially, I thought the best way to do this might be a has_many through: relationship where I had:
Tasks:
class Task < ApplicationRecord
has_many :user_tasks
has_many :users, through: :user_tasks
Users:
class User < ApplicationRecord
has_many :user_tasks
has_many :tasks, through: :user_tasks
And User_Tasks:
class UserTask < ApplicationRecord
belongs_to :user
belongs_to :task
end
The idea was that when a new task is created any user_ids tagged in the task would create new entries in User_Tasks. But I am not 100% sure how to do that in the Task create action and I'm starting to wonder if this is over complicating things?
Is there a simpler way to do this? If not what is the best way to structure a create action around this has_many through: association?