【发布时间】:2014-11-28 12:23:12
【问题描述】:
我想做的是: 我有一个用户模型,我有一个任务模型 任务有两种类型的用户 Owners 和 Supervisors 都是用户!
所以我到目前为止是:
任务模型
class Task < ActiveRecord::Base
has_many :task_owners, dependent: :destroy
has_many :task_supervisors, dependent: :destroy
has_many :users, through: :task_owners
has_many :users, through: :task_supervisors
end
TaskSupervisor 模型
class TaskSupervisor < ActiveRecord::Base
belongs_to :task
belongs_to :user
end
TaskOwner 模型
class TaskOwner < ActiveRecord::Base
belongs_to :task
belongs_to :user
end
最后是用户模型
class User < ActiveRecord::Base
has_many :task_owners
has_many :task_supervisors
has_many :tasks, through: :task_owners
has_many :tasks, through: :task_supervisors
end
现在你可以想象......我的问题是当我得到一个任务并检索用户时,我只得到我的一个关联......我需要的是一种更改吸气剂名称或识别它们的基本方法能够说类似的话
task.owners
task.supervisors
【问题讨论】:
-
你为什么有这个两次?
has_many :users, through: :task_owners。User模型也应该有:has_many :tasks(任务的复数) -
@TheChamp 对不起......复制粘贴问题......我马上清理它
-
至于 has_many 任务,这是一个真正的错误 :)
-
您能否再描述一下这个问题?我不明白您所说的“您只检索一个关联”是什么意思。当您致电
task.task_owners时,这不是预期的吗?
标签: ruby-on-rails ruby associations model-associations