ruby on rails pass a value to .last() - ruby-on-rails

I'm pretty new to ruby on rails, so I'm probably missing some syntax. Big picture I am trying to get the value for a specified percentile. Conceptually I am taking my table 'Scores', sorting it, getting the last 'x' values, and then taking the first value. I can't seem to figure out how to pass 'x', which is based on the length of the dataset to the chain.
def get_percentile()
record_count = Scores.count(:id)*0.05
record_threshold = record_count.round()
Score_percentile = Scores.order(:points).last(record_threshold).first().points
return Score_percentile
end
get_percentile
If I just enter .last(20) this works as I expect, so I just don't know how to pass the variable.
Thanks.

You may be passing a 0 into your .last() function with your rounding.
There are a variety of options to make sure you pass at least a 1
[record_threshold, 1].max will give you at least 1. https://apidock.com/ruby/Enumerable/max
Changing .round() to .ceil() rounds up in all instances. https://apidock.com/ruby/Float/ceil

Related

Replacing empty cells in a variable with values from another

I have a dataset with a number of columns. Two of them are practically the same however in variable column 1 there are string data that I would like to extract and replace in empty cells of variable column 2.
I tried using the syntax
If
variable_2 = "".
Compute variable_1 = variable_2.
End If
But do not get anything. Please, could someone help with this?
Much appreciated.
This should be either
if var2="" var2=var1.
(no period after the condition, no "end if")
OR
do if var2="".
compute var2=var1.
end if.
(this is a "do if" and not just an "if" - enables you to add commands after the condition, and not needed here).
In any case, if variable_2 is empty you want to run variable_2=variable_1 and not the reverse.

Rails hash TypeError

I have the following hash, #example_set that I want to get and store data from.
{"Example1"=>{:campaign=>"Example1", :impressions=>12, :conversions=>1, :clicks=>14,
"Example2"=>{:campaign=>"Example2", :impressions=>4042, :conversions=>2, :clicks=>11}}
I want to do the following to combine the total conversions but am running into a TypeError: no implicit conversion of Symbol into Integer.
#totals = 0
#example_set.each do |report|
#totals += report[:conversions]
end
Ideally this would set #totals to 3
I am new to rails so any additional detail and instruction would be much appreciated (especially if there is a better way to do this.. which I assume there is)
You want to iterate over values of that map, I think.
#example_set.values.each

Storing value of calculation that query result is being ordered by with Rails

I'm new to rails but I still feel I should know the answer to this. Drawing a complete blank.
I have the following query that returns the expected results. However I want to also store the value the calculation they are being ordered by returns.
What is the best way of extracting this value.
Here is the line of code
#service = Service.order("ST_Distance(services.lon_lat, ST_GeomFromText('POINT (lat lon)', 4326))").limit(10)
As I said it returns the correct results but I'd also like to find the distance for each result
Thank you
Try this:
#services = Service.select("*, ST_Distance(services.lon_lat, ST_GeomFromText('POINT (lat lon)', 4326)) as st_distance").order("st_distance").limit(10)
the objects you get back in #services (note i changed this to the plural in keeping with convention) should have an additional method .st_distance, which is what i set the results of that function to be called with as st_distance

Incrementing database value in Rails

In Rails, I'd like to update some database value by incrementing it.
Say, I have value 30 in a table and I'd like to add 0.5.
Here is how I've tried to accomplish it:
Record.where(:status => "somestatus").first.value.to_f += 0.5
RoR complains saying "undefined method `to_f='".
I know that I can obtain value first, calculate the result and update_attributes at the end. But what is the most efficient way to do it?
Hope the field value is of type Float, the following will do the update the value by what ever you want.
object = Record.where(:status => "somestatus").first
object.increment!(:value, 0.5)
Check here for documentation on increment! method.

jRuby / Rails sort hash by another hash value

I have a hash that will render my html differently based on a particular variable. The variable is within the hash. So I am wondering how I can pass a hash value to a group by. to sort the rest heres what I am trying, maybe this will explain it better than me wording it.
<% grouped = svcs.group_by { |svc| svc[val[:sorttype]] } %>
val is a multidimensional hash. the first 2 key value pairs sorttype and one other are simple key and value, the 3rd piece (svcs) contains the equivilent of a 2D hash. Which if I manually type the type of sort I want to apply to it for the group by it works ie:
<% grouped = svcs.group_by { |svc| svc[:service_name] } %>
in PHP i know in a similar instance I can pass a variable of some sort to something like this and have it work. I assume such is the case here. However Im not sure how to put the variable in. Cause all the ways Ive tried don't work
It depends a little.
Rails' has a HashWithIndifferentAccess that will not distinguish between string and symbol keys; if you're using one of those, it should work as-is.
If it's not, it depends what the val entries are--if they're strings, convert to a symbol using to_sym, e.g., svc[val[:sorttype].to_sym].

Resources