has_many通过多条件关联

时间:2015-11-16 17:36:38

标签: ruby-on-rails ruby activerecord

我有客户,作家和故事。

出版的作家是客户与作家之间通过故事被接受的故事之间的关联。我可以在writer id上做一个uniq来为客户提供已发布的编写器。

代码如下:

has_many :accepted_stories, -> { accepted }, class_name: Story

has_many :published_writers, ->{ uniq(:writer_id) }, through: :accepted_stories, source: :writer, class_name: Writer

现在我想要获得批准率高的出版作家。这意味着至少有50%的故事被该客户接受,因为客户也可以拒绝故事。

所以我想尝试写下面的内容,但我不确定如何。

has_many :accepted_or_rejected_stories, -> { rejected || accepted }, class_name: Story

has_many :published_writers_with_high_approval, ->{ where sum accepted stories are >= sum rejected stories }, through: :accepted_or_rejected_stories, source: :writer, class_name: Writer

0 个答案:

没有答案