Ruby on rails 查询模型实例是否具有任何指定的关联集

Ruby on rails 查询模型实例是否具有任何指定的关联集,ruby-on-rails,associations,Ruby On Rails,Associations,我想为一个模型(在本例中是参与者模型)编写一个方法,该方法查看它的一组关联,并简单地记录是否存在任何关联。以下是我所拥有的: def post_screener_associations? ParticipantAuthorizationForm.where(:participant_id => self.id).count > 0 ParticipantConsent.where(:participant_id => self.id).count >

我想为一个模型(在本例中是参与者模型)编写一个方法,该方法查看它的一组关联,并简单地记录是否存在任何关联。以下是我所拥有的:

  def post_screener_associations?
    ParticipantAuthorizationForm.where(:participant_id => self.id).count > 0
    ParticipantConsent.where(:participant_id => self.id).count > 0
    # and so on exactly like the format above about 8 more times!
  end
我知道有更好的方法来编写这个查询,但我不想打扰我的同事。谢谢

  def post_screener_associations?
    self.class.reflect_on_all_associations.all? { |a| send(a.name).present? }
  end

这应该询问每个关联是否存在,如果all present方法返回true,则在另一种情况下将返回false,以防您不想使用反射。它类似于您原来的帖子,使用参与者模型上的活动记录方法简化了它

def post_screeener_associations?
  participant_authorization_form.present? ||
  participant_consent.present? ||
  # etc.
end

谢谢。这才是我真正想要的。我知道有一种更干净的方法可以得到结果。