Rails UNIQ方法不起作用 - Rails 4

时间:2016-06-28 14:21:19

标签: ruby-on-rails-4

我正在尝试使用rails helper方法pow(a, b),但不确定它为什么不起作用。非常感谢您的帮助

  • 我有申请表格清单,例如:3表格
  • 申请表格中有2份是女性,1份是男性
  • 2份申请表格由同一名女士
  • uniq = @forms.count
  • 3 = @forms.female_applicants.count
  • 2 =给我@forms.female_applicants.uniq.count,但答案应为2
  

模型

  • 1属于Form
  • user有很多User
  • forms属于User
  • category_gender有很多Category_gender
  

Form.rb

users
  

我的问题:如果同一个女人有2个表格,可以请一个人告诉我如何   将表单数量唯一地显示为1而不是2.所以当   查询scope :female_applicants, ->() { joins(:category_gender).where('category_genders.name' => "Female") } 结果为@forms.female_applicants.uniq.count   而不是1如果有比使用2方法更好的方法,我会非常感激,如果被告知

1 个答案:

答案 0 :(得分:1)

@Dan& @hypern你是对的 - 存在非独特元素,这就是为什么我不能使用rails方法uniq

使用@forms.female_applicants.group_by(&:user).count导致了我正在寻找的答案

非常感谢