1

我有以下三个模型

法官(设计用户)

Judge.column_names
=> ["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "created_at", "updated_at", "confirmation_token", "confirmed_at", "confirmation_sent_at", "unconfirmed_email"]

class Judge < ActiveRecord::Base
   has_many :votes
end

入口

Entry.column_names
 => ["id", "entrant_id", "title_of_work", "price", "created_at", "updated_at", "media_file_name", "media_content_type", "media_file_size", "media_updated_at", "paid", "height", "width", "type_of_media"]

class Entry < ActiveRecord::Base
    belongs_to :entrant
    has_many :votes
end

投票

Vote.column_names
=> ["id", "entry_id", "judge_id", "score", "created_at", "updated_at"]

class Vote < ActiveRecord::Base
    belongs_to :entry
    belongs_to :judge
end

我需要一个执行以下操作的查询

如果我以评委身份登录,请选择一个付费入场的人,该人还没有我已经给予的投票。

我有这个,但它似乎不起作用

Entry.where(:paid => 1).includes(:votes).where.not(votes: { id: nil }).where.not(votes: {judge_id: current_judge.id }).first

我正在尝试创建一个显示可以投票的“条目”的页面。一旦投票,投票表中就会有一条记录,并且永远不会再显示。有很多法官,因此这些规则中的每一个都适用于每位法官。

有没有更好的方法来做到这一点?

4

2 回答 2

1

这可能会解决您的问题

Entry.includes(:votes).where(paid: 1).where("votes.judge_id != ? ", current_judge.id)
于 2015-05-22T11:36:25.750 回答
0

如果您的意思是说您是法官并想要获取已付费且您没有任何投票的条目?然后以下查询可能会对您有所帮助: Entry.includes(:vote).where('paid = 1 and votes.judge_id <> ?', current_judge.id).references(:votes)

于 2015-05-22T11:29:57.730 回答