0

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?

1
  • try select_one instead of execute Commented Mar 25, 2012 at 16:14

2 Answers 2

2

[{"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']
Sign up to request clarification or add additional context in comments.

Comments

0

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']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.