2012-02-10 2 views
0

私は問題がありますが、それがfind_by_sql、配列オブジェクトのあるもの、または自分のロジックであるかどうかはわかりません。次のコードはヘルパーにあります。このアプリは4象限の座標グリッドを持っています。私はアプリの名前をSomethingに置き換えたので、その参照は現時点でアプリを保護するためのものです。私はこのコードが畳み込まれている可能性があることを知っていますが、私は何かの前にまずそれを働かせたいと思いました。基本的には、問題は次のとおりです.SQLクエリを実行すると、私が期待している結果が得られます。同じ親の子である座標にヒットすると、それらのうちの1つが正しく描画されますが、削除に達すると、配列から両方の座標が削除されます。違いがある場合は、結合テーブルのIDが親座標のIDとして返されます。私は必要なものだけを選択しようとしましたが、それは助けになりませんでした(このスレッドの解決策に従って:http://www.sitepoint.com/forums/showthread.php?415007-rails-join-creates-wrong-id)。どんな助けでも大歓迎です。find_by_sqlまたは配列の問題

さらに詳述すると、象限には16の親座標があります。それが空白の場合は空白を描画し、空でない場合はすべての子を収集するdivを描画する必要があります(最初のelse ifの場合)。次に描画するはずですそこにいるすべての子供たち、そしてdivを閉めて移動しています。お役に立てれば。

def buildQuadrantForUser(options={}) 
buffer="" 

user_coordinates = SomethingUser.find_by_sql(' 
Select * from something_users 
inner join coordinates as a on 
`something_users`.coordinate_id = `a`.id 
inner join coordinates as b on 
b.id = a.ancestry 
Where ((user_id = '+options[:user].id.to_s+') AND (visibility = 2) AND (a.quadrant = '+options[:quadrant].to_s+')) 
Order By b.number ASC 
') 

i=0 
while i<16 do 
    i+=1 
    first = true 
    drawn = false 
    l3num = 0 
    user_coordinates.collect{|coor| 
    puts "quadrant #{options[:quadrant].to_s}" 
    if !coor.number.to_i.eql?(i) 
     puts "quadrant: #{options[:quadrant].to_s}, number: #{i.to_s}, coordinate #{coor.id}" 
     if drawn == false 
     buffer<<"<div id=q"+options[:quadrant].to_s+"_"+i.to_s+" class='l2_div sc0'>" 
     buffer<<"</div>" 
     drawn = true 
     end 
    else 
     puts "quadrant: #{options[:quadrant].to_s}, number: #{i.to_s}, coordinate #{coor.inspect}" 
     drawn = true 
     if first == true 
     buffer<<"<div id=q"+options[:quadrant].to_s+"_"+coor.number.to_s+" class='l2_div sc#{coor.coordinate.parent.percent_clicks_user_children(:user=>options[:user])}' data-value=#{coor.coordinate.parent.name} something-rating=#{coor.coordinate.parent.id.to_s}>" 
     first = false 
     end#end first 
     l3num = l3num + 1 
     if coor.coordinate.static? 
     if !current_user.blank? && coor.user_id == current_user.id 
      buffer<<content_tag(:div, content_tag(:span, "", :id=>'You'),:class=>"l3_#{l3num.to_s} cic#{coor.coordinate.percent_clicks_user(:user=>options[:user])}", :user=>'You', :somethingsomething=>coor.something_id.to_s,:something=>coor.something_id.to_s,:id=> "e" + coor.something_id.to_s, :rating=>coor.coordinate.name.to_s, :tag=>coor.something.tags.collect{|tag| tag.name+","}, :date=>time_ago_in_words(coor.updated_at), :source=>coor.something.url.split('/')[2], :link=>coor.something.url) 
     else 
      buffer<<content_tag(:div, content_tag(:span, "", :id=>coor.user.name),:class=>"l3_#{l3num.to_s} cic#{coor.coordinate.percent_clicks_user(:user=>options[:user])}", :user=>coor.user.name, :something=>coor.something_id.to_s,:id=> "e" + coor.something_id.to_s, :rating=>coor.coordinate.name.to_s, :tag=>coor.something.tags.collect{|tag| tag.name+","}, :date=>time_ago_in_words(coor.updated_at), :source=>coor.something.url.split('/')[2], :link=>coor.something.url) 
     end 
     else 
     l3num = l3num-1 if l3num !=0 
     if !current_user.blank? && coor.user_id == current_user.id 
      buffer<<content_tag(:div, content_tag(:span, "", :id=>'You'),:class=>"l3_5 cic#{coor.coordinate.percent_clicks_user(:user=>options[:user])}", :user=>'You', :user=>coor.something.title,:something=>coor.something_id.to_s,:id=> "e" + coor.something_id.to_s, :rating=>coor.coordinate.name.to_s, :tag=>coor.something.tags.collect{|tag| tag.name+","}, :date=>time_ago_in_words(coor.updated_at), :source=>coor.something.url.split('/')[2], :link=>coor.something.url) 
     else 
      buffer<<content_tag(:div, content_tag(:span, "", :id=>coor.user.name),:class=>"l3_5 cic#{coor.coordinate.percent_clicks_user(:user=>options[:user])}", :user=>coor.something.title, :something=>coor.something_id.to_s,:id=> "e" + coor.something_id.to_s, :rating=>coor.coordinate.name.to_s, :tag=>coor.something.tags.collect{|tag| tag.name+","}, :date=>time_ago_in_words(coor.updated_at), :source=>coor.something.url.split('/')[2], :link=>coor.something.url) 
     end 
     end#end static 
    buffer<<"</div>" 
    puts "deleting #{coor.inspect}" 
    user_coordinates.delete(coor) 
    end#end coordinate.number = i.to_s 
    } 

end#end while 
return buffer 

エンド

答えて

0

すべてのIDが選択されないように制限することで、数時間の作業後にこれを修正することができます。私は何も削除しないで終わり、そのコードの多くを再構成しなければならなかった。彼らの参加の中で奇妙なIDを把握しようとする人のために、あなたが選んでいるものを間違いなく制限する。

2

Array#deleteは、あなたがそれを与える引数に等しい任意のオブジェクトを削除します。

アクティブレコードオブジェクトの場合、等価は同じIDを持つものとして定義されます。

あなたはselect *を行っているが、アクティブなレコードはtable1.idtable2.idなどを見ていない - あなたは結果セットからのID列のいずれかを取得しますときに、あまりにもcoor.idの列は、互いに影。その後、あなたはb.idが持っていたすべての行を削除します配列からその行を削除すると、アクティブなレコードが任意にそのIDをするb.idを選んだこと、たとえば言う - このことを考えると

ことが明らかに悪いことだ、平等を定義するものですその値。あなたが選んだもので遊んでいたのは、各オブジェクトのIDが異なるように選択しない限り、Array#deleteボールが再生されないことです。それを反復する。

+0

ええ、私は本当に何をすべきか分かりません。私はそれが2つの問題の組み合わせであると考えました。私は16とコレクションの両方を反復しなければならないことを知っているので、コレクションから取り除くと、スピードアップに役立ちます。私は現在のオブジェクトが常に存在するのでdelete.at(0)を使ってみましたが、それでも私のためには動作しません。他のアイデア?どのようにIDを修正するには、削除を修正するには?私はこの数日間、これに対して頭を傷つけてきた。 – pash

+0

selectをsomething_users。*、a.name、a.ancestry、b.idをsid、b.name、b.numberからsomething_usersに変更しました。私は今、正しいIDを取得していますが、それでもすぐに2つのオブジェクトを削除しています – pash

+0

なぜあなたは全く削除する必要がありますか? –