I’m having issues and I can’t tell if it’s with find_by_sql, something with the array object, or my logic. The following code is in a helper. This app has a coordinate grid with 4 quadrants. I replaced the app name with Something, so any references with that are to protect the app at the moment. I know this code may be convoluted, but I wanted to get it working first before anything else. Basically, the problem is this: When I run the SQL query, I get the results I expect back. When it hits coordinates that are children of the same parent, it’ll draw one of them correctly, but when it gets to the delete, it will delete both coordinates from the array. If it makes a difference, the join table ids are getting returned as the parent coordinate’s id. I tried selecting only what I needed, and that didn’t help (as per the solution from this thread: http://www.sitepoint.com/forums/showthread.php?415007-rails-join-creates-wrong-id). Any help would be greatly appreciated.
To further elaborate, there are 16 parent coordinates in a quadrant. If it’s blank, we draw a blank one (as per the first if), if it is not blank, we have to draw a div to collect all the children (first if of the else), then, we are supposed to be drawing all the children in there, and closing off the div and moving on. Hope this helps.
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
end
Managed to get this fixed after a few more hours of work by restricting all ids from being selected. I ended up not deleting anything and had to restructure a lot of that code. For those trying to figure out the weird IDs in their join, definitely restrict what you’re selecting.