Ruby on rails Ruby:specify has\u many:through关联(当给定多个has\u many:through关联时)
我有一个简单的数据设置,包括用户模型和任务模型。 在这两种模式中,我有两种:通过协会:奖学金和分配。总的来说,我想为一项任务指定几个追随者和几个受让人 现在我想显示一个特定任务的所有被分配者和所有追随者。 如果只有一个关联,我就可以做@task.users。因为我有两个关联,所以我想指定通过哪个关联获取所有用户 请参阅我的代码:Ruby on rails Ruby:specify has\u many:through关联(当给定多个has\u many:through关联时),ruby-on-rails,ruby,activerecord,has-many-through,Ruby On Rails,Ruby,Activerecord,Has Many Through,我有一个简单的数据设置,包括用户模型和任务模型。 在这两种模式中,我有两种:通过协会:奖学金和分配。总的来说,我想为一项任务指定几个追随者和几个受让人 现在我想显示一个特定任务的所有被分配者和所有追随者。 如果只有一个关联,我就可以做@task.users。因为我有两个关联,所以我想指定通过哪个关联获取所有用户 请参阅我的代码: class User < ActiveRecord::Base has_many :assignments has_many :tasks, th
class User < ActiveRecord::Base
has_many :assignments
has_many :tasks, through: :assignments
has_many :fellowships
has_many :tasks, through: :fellowships
end
class Task < ActiveRecord::Base
has_many :assignments
has_many :users, through: :assignments
has_many :fellowships
has_many :users, through: :fellowships
end
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :task
end
class Fellowship < ActiveRecord::Base
belongs_to :user
belongs_to :task
end
我现在想让所有的受让人和追随者
@assignees=@task.users“超过关联分配”
@followers=@task.users“超过关联跟踪”
但我不知道怎么做
谢谢你的帮助 你可以用下面的方式写 有很多:分配任务,通过::分配,源::任务 有很多:团契任务,通过::团契,来源::任务
因此,如果我将这些助理任务和团契任务添加到我的模型关联中,那么我可以使用
@assignees=@task.assignment\u用户和@followers=@task.assignment\u用户?使用assignees
和followers
来代替任务和团契任务吗。这样,您就可以使用@task.followers
和@task.assignees
获取。
@task = Task.first