Active Record 查询 select 个条目,该条目还没有我已经投过的票
Active Record Query to select an entry, that does not already have a vote that I have already given
我有以下三个模型
判断(设计用户)
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
我需要执行以下操作的查询
如果我以法官的身份登录,select 一个人支付了入场费,那还没有我已经投过的票。
我有这个,但它似乎不起作用
Entry.where(:paid => 1).includes(:votes).where.not(votes: { id: nil }).where.not(votes: {judge_id: current_judge.id }).first
我正在尝试创建一个显示可以投票的 'entry' 的页面。投票后,投票 table 中将有一条记录,并且永远不会再次显示。有很多法官,所以这些规则中的每一个都适用于每个法官。
有更好的方法吗?
如果你的意思是说你是法官,想要获取已付费但没有任何投票的参赛作品?那么以下查询可能会对您有所帮助:Entry.includes(:vote).where('paid = 1 and votes.judge_id <> ?', current_judge.id).references(:votes)
这可能会解决您的问题
Entry.includes(:votes).where(paid: 1).where("votes.judge_id != ? ", current_judge.id)
我有以下三个模型
判断(设计用户)
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
我需要执行以下操作的查询
如果我以法官的身份登录,select 一个人支付了入场费,那还没有我已经投过的票。
我有这个,但它似乎不起作用
Entry.where(:paid => 1).includes(:votes).where.not(votes: { id: nil }).where.not(votes: {judge_id: current_judge.id }).first
我正在尝试创建一个显示可以投票的 'entry' 的页面。投票后,投票 table 中将有一条记录,并且永远不会再次显示。有很多法官,所以这些规则中的每一个都适用于每个法官。
有更好的方法吗?
如果你的意思是说你是法官,想要获取已付费但没有任何投票的参赛作品?那么以下查询可能会对您有所帮助:Entry.includes(:vote).where('paid = 1 and votes.judge_id <> ?', current_judge.id).references(:votes)
这可能会解决您的问题
Entry.includes(:votes).where(paid: 1).where("votes.judge_id != ? ", current_judge.id)