Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何在Sinatra上使用Sequel with Postgres避免连接查询中的重复?_Ruby_Postgresql_Sinatra_Sequel - Fatal编程技术网

Ruby 如何在Sinatra上使用Sequel with Postgres避免连接查询中的重复?

Ruby 如何在Sinatra上使用Sequel with Postgres避免连接查询中的重复?,ruby,postgresql,sinatra,sequel,Ruby,Postgresql,Sinatra,Sequel,我想做一个简单的连接。我有两张桌子:候选人和笔记 并不是所有的候选人都写了关于他们的笔记,有些候选人写了不止一张关于他们的笔记。链接字段是候选表中的id和notes表中的候选_id。查询是: people = candidates.where(:industry => industry).where("country = ?", country).left_outer_join(:notes, :candidate_id => :id).order(Sequel.desc(:id))

我想做一个简单的连接。我有两张桌子:候选人和笔记

并不是所有的候选人都写了关于他们的笔记,有些候选人写了不止一张关于他们的笔记。链接字段是候选表中的id和notes表中的候选_id。查询是:

people = candidates.where(:industry => industry).where("country = ?", country).left_outer_join(:notes, :candidate_id => :id).order(Sequel.desc(:id)).map do |row|
  {
    :id => row[:id],
    :first => row[:first],
    :last => row[:last],
    :designation => row[:designation],
    :company => row[:company],
    :email => row[:email],
    :remarks => row[:remarks],
    :note => row[:note]
  } 
end
它工作得很好,从候选者表中获取所有指定的候选者,并从notes表中获取注释,但是如果有多个注释,它会重复候选者的名称。在结果列表中,根据与此人关联的注释数量,此人abc出现两次或三次

我实际上并没有打印HTML结果中的注释,只是在那个人有注释时打勾,如果没有注释,则打勾

我希望这个人的名字只出现一次。我曾尝试在查询中每个可以想到的地方添加distinct,但没有效果


有什么想法吗?

为了使distinct有效,您需要确保您只选择了要在其上进行distinct的列。您可以尝试将其添加到查询中

.select(:candidates__id, :first, :last, :designation, :company, :email, :remarks, Sequel.as({:notes=>nil}).as(:notes)).distinct
但如果您使用的是一个像样的数据库,那么最好使用subselect而不是join来检查notes的存在性:

candidates.where(:industry => industry, :country=>country).select_append(Sequel.as({:id=>DB[:notes].select(:candidate_id)}, :note)).order(Sequel.desc(:id)).map do |row|
  { :id => row[:id], :first => row[:first], :last => row[:last], :designation => row[:designation], :company => row[:company], :email => row[:email], :remarks => row[:remarks], :note => row[:note] } 
end