Formatting results from SQL in Ruby On Rails - ruby-on-rails

I have a custom SQL query that I run with this line:
#avg_score = "#{ActiveRecord::Base.connection.execute(#avg_score_SQL)}"
when that is run, #avg_score has a value of:
[{"score"=>8, 0=>8}]
the results are correct, but it prints it like that :-/ I just want it to print out the "8"
How do I get it to just print the value of score?

[{"score"=>8, 0=>8}] is just an array with one element, and that element is a hash. So you can access the score like this:
#avg_score[0]['score']

if you use execute method, you have to free the Result object after you're done using it.
Also your statement can be used without quotes.
if you always have 1 result row, you should better use selecy_one
#avg_score = ActiveRecord::Base.connection.select_one(#avg_score_SQL)['score']

Related

How do I remove double square brackets in ruby on rails?

After converting sql query from mysql object to json format I am getting array and I want only integer part from the array. Please let me know how can I achieve that.
query = "select count(priority) from table_complaint"
data = ActiveRecord::Base.connection(query).to_json
result is [[55]]
After executing above query I am getting results as [[integer]]
Now I want only integer/float part from the above array. Please help me out
You could take the first element of the outer array, which will be the inner array, and then the first element of that.
[[55]][0][0]
You might find the MiniSQL gem to be useful, but I wonder why you do not:
data = TableComplaint.count

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.

Print ResponseIDs of missing values SPSS

I'm creating a variable that will hold missing values from a specific variable. Currently, this works but it gives the missing a value a 1. How do I tell spss to print the respondent's ResponseID instead?
My code below:
COMPUTE Q_2_MIS = MISSING(Q_2).
EXECUTE.
Thanks
Your code returns value of 1 because the condition missing(q_2) is evaluated to TRUE.
Try this:
DO IF MISSING(Q_2).
COMPUTE Q_2_MIS = ResponseID .
END IF.
EXECUTE.
or (as per eli-k's comment) simply use IF:
IF MISSING(Q_2) Q_2_MIS = ResponseID .
EXECUTE.
Note that you might need to create the Q_2_MIS variable first, if you do not have it in your dataset.
Alternatively, if you want to print out the IDs of the respondents with missing in Q_2:
TEMPORARY.
SELECT IF missing(q_2).
LIST ResponseID q_2.
You will see a list of IDs in the SPSS Output, with a (blank) Q_2 next to each ID.

Eql command returns an empty array when using two parameters

When I am running the command like this:
related_attchments = related_documents.collect{|d| d.attachments.all}.flatten.select{|a| [“a”,“b”].eql?(a.attachment_type)}
the above command it is returning empty []
so basically I want to know whether I can use .eql? function with two parameters or not.
Because above statement works fine if i pass in below way:
related_attchments = related_documents.collect{|d| d.attachments.all}.flatten.select{|a| "a".eql?(a.attachment_type)}
Use include, like this:
related_attchments = related_documents.collect{|d| d.attachments.all}.flatten.select{|a| [“a”,“b”].include?(a.attachment_type)}
For your case use include? instead of eql?
.eql? use only one parameter, but on the other hand it can be an array as well. In your case ["a","b"] is comparing only to "a", or "b", so it will never be equal. If it would be the ["a","b"], then it would be equal. So, for your case use include? instead of eql as it will represent the comparison which you want to achieve.

Google Spreadsheet long IF statement?

I have this statement:
=if(
F1B!D3="1",50+FLOOR(D2/10,1),
if(F1B!D3="2",40),
if(F1B!D3="3",30),
if(F1B!D3="4",25),
if(F1B!D3="5",20),
if(F1B!D3="6",19),
if(F1B!D3="7",18),
if(F1B!D3="8",17),
if(F1B!D3="9",16),
if(F1B!D3="10",15),
if(F1B!D3="11",14),
if(F1B!D3="12",13),
if(F1B!D3="13",12),
if(F1B!D3="14",11),
if(F1B!D3="15",10),
if(F1B!D3="16",9),
if(F1B!D3="17",8),
if(F1B!D3="18",7),
if(F1B!D3="19",6),
if(F1B!D3="20",5),
if(F1B!D3="21",4),
if(F1B!D3="22",3),
if(F1B!D3="23",2),
if(F1B!D3="24",1));
But GoogleDocs return me "error: Wrong number of arguments to IF"
What I'am doing wrong?
You can't pass infinitely many arguments to IF. There's a single condition, a single "THEN", and a single "ELSE". You need to "nest" your IF statements, where each new IF() in part of the previous IF statement's ELSE. Something like this (abbreviated):
=if(
F1B!D3="1",50+FLOOR(D2/10,1),
if(F1B!D3="2",40,
if(F1B!D3="3",30,
if(F1B!D3="4",25,
if(F1B!D3="5",20,
if(F1B!D3="6",19,
if(F1B!D3="7",18)))))))
Trying to apply too many IFs, far more than necessary:
=IF(F1B!D3=1,50+FLOOR(F1B!D2/10,1),iferror(CHOOSE(F1B!D3-1,40,30,25),25-F1B!D3))
Also do not append ;.

Resources