【发布时间】:2013-09-26 02:43:56
【问题描述】:
我有两个关联的模型:
class Facility < ActiveRecord::Base
has_and_belongs_to_many :investigators, class_name: "Person"
has_and_belongs_to_many :technicians, class_name: "Person"
end
和
class Person < ActiveRecord::Base
has_and_belongs_to_many :facilities
end
如果我同时实例化调查员和技术员,我可以使用例如获取与特定设施相关联的每个人的数量
numtechs = myfacility.technicians.size
numinvests = myfacility.investigators.size
等等。但是我如何返回与设施相关联的总人数。尝试
numpeople = myfacility.people.size
似乎不起作用。
有没有办法一举做到这一点?
谢谢。
【问题讨论】:
-
等等!我刚刚重新阅读了我最初的帖子-第二个模型应该说'has_and_belongs_to_many:facilities'。我现在就编辑它
标签: ruby-on-rails associations has-and-belongs-to-many