find_by_sql or array issue - ruby-on-rails

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

Array#delete removes any objects that are equal to the argument you give it.
For Active Record objects, equality is defined as having the same id.
You are doing select * but active record doesn't see table1.id, table2.id etc. - the columns shadow each other so when you too coor.id you'll get one of the id columns from the result set.
Given that this is what defines equality, that's obviously a bad thing - say for example that active record arbitrarily picked b.id to be its id, then when you remove that row from the array you'll remove every row where b.id had that value. I'm guessing you've been playing around with what you select, unless you select something such that each object has a different id Array#delete won't play ball
Also, you really shouldn't modify a collection while you are iterating over it.

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.

Related

ActiveRecord next item

I am currently getting back a list of items in a prioritized order:
#priotizedorder = #categories.order(:priority)
What I want to create is a button that allows a user to get to the next category from an item. I'm trying to do that by saying:
#activecategory = #categories.where(:name => params[:cat]).first
#nextcategory = #priotizedorder.select {|item|'name' == #activecategory}
But that's returning an empty array:
[]
How do I select the next category in an active record?
Firstly, your question is not clear to me, you are asking for next category but you are searching the given category in the order, according to my knowledge I am providing the solution
Your statement must be
#nextcategory = #priotizedorder.select {|item| item == #activecategory}
When you are using 'select', above statement returns an array of results matching with the value, you should take the first value from the array.
i.e., #nextcategory = #priotizedorder.select {|item| item == #activecategory}.first
(OR) You can use find method
#nextcategory = #priotizedorder.find {|item| item == #activecategory}
If you want to select the next prioritised category of given item, then use the below code
#currentcategory = #priotizedorder.find {|item| item == #activecategory}
#currentcategory_index = #priotizedorder.index(#currentcategory)
#nextcategory = #priotizedorder[#currentcategory_index+1]

Lua - Table.hasValue returning nil

I have a table something like this:
table = {milk, butter, cheese} -- without "Quotation marks"
I was searching for a way to check if a given value is in the table or not, and found this:
if table.hasValue(table, milk) == true then ...
but it returns nil, any reason why? (it says .hasValue is invalid) or can I get an alternative to check if value exists in that table? I tried several ways like:
if table.milk == true then ...
if table[milk] == true then ...
All of these returns nil or false.
you can try this
items = {milk=true, butter=true, cheese=true}
if items.milk then
...
end
OR
if items.butter == true then
...
end
A Lua table can act as an array or as an associative array (map).
There is no hasValue, but by using a table as an associative array you can easily implement it efficiently:
local table = {
milk = true,
butter = true,
cheese = true,
}
-- has milk?
if table.milk then
print("Has milk!")
end
if table.rocks then
print("Has rocks!")
end
You have a few options here.
One, is to create a set:
local set = {
foo = true,
bar = true,
baz = true
}
Then you check if either of these are in the table:
if set.bar then
The drawback to this approach is that you won't iterate over it in any specific order (pairs returns items in an arbitrary order).
Another option would be to use a function to check each value in a table. This'll be very slow in large tables, which brings us to back to a modification of the first option: A reverse lookup generator: (This is what I'd recommend doing -- unless your set is static)
local data = {"milk", "butter", "cheese"}
local function reverse(tbl, target)
local target = target or {}
for k, v in pairs(tbl) do
target[v] = k
end
return target
end
local revdata = reverse(data)
print(revdata.cheese, revdata.butter, revdata.milk)
-- Output: 3 2 1
This'll generate a set (with the added bonus of giving you the index where the value was in your original table). You can also put the reverse into the same table as the data was in, but this won't go well with numbers (and it'll be messy if you need to generate the reverse again).
If you write table = {milk=true, butter=true, cheese=true}, then you can use if table.milk == true then ....

Check if string contains element in Array

I'm using Rails and learning ActiveRecord and I came across a vexing problem. Here's an array in my model:
#sea_countries = ['Singapore','Malaysia','Indonesia', 'Vietnam', 'Philippines', 'Thailand']
And here's my ActiveRecord object:
#sea_funding = StartupFunding.joins(:startup)
.where('startups.locations LIKE ?', '%Singapore%')
What I'm trying to do is to return a result where a string in the 'locations' column matches any element in the Array. I'm able to match the strings to each element of an Array (as above), but I'm not sure how to iterate over the whole Array such that the element is included as long as there's one match.
The intent is that an element with multiple locations 'Singapore,Malaysia' would be included within #sea_funding as well.
Well, don't ask me why 'locations' is set as a string. It's just the way the previous developer did it.
You use an IN clause in your .where filter:
#sea_funding = StartupFunding.joins(:startup)
.where(["startups.locations IN (?)", #sea_countries])
#sea_countries.include?(startups.locations)
This will return a boolean TRUE if the value of the locations column in startups can be found in the sea_countries array, false if it is absent.
Could this work for you?
first = true
where_clause = nil
sea_countries.each do |country|
quoted_country = ActiveRecord::Base.connection.quote_string(country)
if first
where_clause = "startups.locations LIKE '%#{quoted_country}%' "
first = false
else
where_clause += "OR startups.locations LIKE '%#{quoted_country}%' "
end
end
#sea_funding = StartupFunding.joins(:startup)
.where(where_clause)

Trying to compare all entries of one table in Lua

Basically I have a table of objects, each of these objects has one particular field that is a number. I'm trying to see if any of these numerical entries match up and I can't think of a way to do it. I thought possibly a double for loop, one loop iterating through the table, the other decrementing, but won't this at some point lead to two values being compared twice? I'm worried that it may appear to work on the surface but actually have subtle errors. This is how I pictured the code:
for i = #table, 1, -1 do
for j = 1, #table do
if( table[i].n == table[j].n ) then
table.insert(table2, table[i])
table.insert(table2, table[j])
end
end
end
I want to insert the selected objects, as tables, into another pre made one without any duplicates.
Let the outer loop run over the table, and let the inner loop always start one element ahead of the outer one - this avoids double counting and comparing objects with themselves. Also, if you call the table you want to examine table that will probably hide the table library in which you want to access insert. So let's say you call your input table t:
for i = 1, #t do
for j = i+1, #t do
if( t[i].n == t[j].n ) then
table.insert(table2, t[i])
table.insert(table2, t[j])
end
end
end
Still, if three or more elements have the same value n you will add some of them multiple times. You could use another table to remember which elements you've already inserted:
local done = {}
for i = 1, #t do
for j = i+1, #t do
if( t[i].n == t[j].n ) then
if not done[i] then
table.insert(table2, t[i])
done[i] = true
end
if not done[j] then
table.insert(table2, t[j])
done[j] = true
end
end
end
end
I admit this isn't really elegant, but it's getting late over here, and my brain refuses to think of a neater approach.
EDIT: In fact... using another table, you can reduce this to a single loop. When you encounter a new n you add a new value at n as the key into your helper table - the value will be that t[i] you were just analysing. If you encounter an n that already is in the table, you take that saved element and the current one and add them both to your target list - you also replace the element in the auxiliary table with true or something that's not a table:
local temp = {}
for i = 1, #t do
local n = t[i].n
if not temp[n] then
temp[n] = t[i]
else
if type(temp[n]) == "table" then
table.insert(table2, temp[n])
temp[n] = true
end
table.insert(table2, t[i])
end
end

ruby looping question

I want to make a loop on a variable that can be altered inside of the loop.
first_var.sort.each do |first_id, first_value|
second_var.sort.each do |second_id, second_value_value|
difference = first_value - second_value
if difference >= 0
second_var.delete(second_id)
else
second_var[second_id] += first_value
if second_var[second_id] == 0
second_var.delete(second_id)
end
first_var.delete(first_id)
end
end
end
The idea behind this code is that I want to use it for calculating how much money a certain user is going to give some other user. Both of the variables contain hashes. The first_var is containing the users that will get money, and the second_var is containing the users that are going to pay. The loop is supposed to "fill up" a user that should get money, and when a user gets full, or a user is out of money, to just take it out of the loop, and continue filling up the rest of the users.
How do I do this, because this doesn't work?
Okay. What it looks like you have is two hashes, hence the "id, value" split.
If you are looping through arrays and you want to use the index of the array, you would want to use Array.each_index.
If you are looping through an Array of objects, and 'id' and 'value' are attributes, you only need to call some arbitrary block variable, not two.
Lets assume these are two hashes, H1 and H2, of equal length, with common keys. You want to do the following: if H1[key]value is > than H2[key]:value, remove key from H2, else, sum H1:value to H2:value and put the result in H2[key].
H1.each_key do |k|
if H1[k] > H2[k] then
H2.delete(k)
else
H2[k] = H2[k]+H1[k]
end
end
Assume you are looping through two arrays, and you want to sort them by value, and then if the value in A1[x] is greater than the value in A2[x], remove A2[x]. Else, sum A1[x] with A2[x].
b = a2.sort
a1.sort.each_index do |k|
if a1[k] > b[k]
b[k] = nil
else
b[k] = a1[k] + b[k]
end
end
a2 = b.compact
Based on the new info: you have a hash for payees and a hash for payers. Lets call them ees and ers just for convenience. The difficult part of this is that as you modify the ers hash, you might confuse the loop. One way to do this--poorly--is as follows.
e_keys = ees.keys
r_keys = ers.keys
e = 0
r = 0
until e == e_keys.length or r == r_keys.length
ees[e_keys[e]] = ees[e_keys[e]] + ers[r_keys[r]]
x = max_value - ees[e_keys[e]]
ers[r_keys[r]] = x >= 0 ? 0 : x.abs
ees[e_keys[e]] = [ees[e_keys[e]], max_value].min
if ers[r_keys[r]] == 0 then r+= 1 end
if ees[e_keys[e]] == max_value then e+=1 end
end
The reason I say that this is not a great solution is that I think there is a more "ruby" way to do this, but I'm not sure what it is. This does avoid any problems that modifying the hash you are iterating through might cause, however.
Do you mean?
some_value = 5
arrarr = [[],[1,2,5],[5,3],[2,5,7],[5,6,2,5]]
arrarr.each do |a|
a.delete(some_value)
end
arrarr now has the value [[], [1, 2], [3], [2, 7], [6, 2]]
I think you can sort of alter a variable inside such a loop but I would highly recommend against it. I'm guessing it's undefined behaviour.
here is what happened when I tried it
a.each do |x|
p x
a = []
end
prints
1
2
3
4
5
and a is [] at the end
while
a.each do |x|
p x
a = []
end
prints nothing
and a is [] at the end
If you can I'd try using
each/map/filter/select.ect. otherwise make a new array and looping through list a normally.
Or loop over numbers from x to y
1.upto(5).each do |n|
do_stuff_with(arr[n])
end
Assuming:
some_var = [1,2,3,4]
delete_if sounds like a viable candidate for this:
some_var.delete_if { |a| a == 1 }
p some_var
=> [2,3,4]

Resources