nafkot
nafkot

Reputation: 113

ruby on rails store sql result on a variable

Newbie here. I am trying to store the result of my search onto a variable.

@answer = q.answers.select(:name) which runs 
"SELECT name FROM "answers" WHERE "answers"."question_id" = 1;" and returns 
"t" for true.

It runs fine on the command line and shows the right result. But I want to compare that result to another variable.

How do i extract that result? @answer[0], or @answer, or answer_var = @answer[0] i.e.

if @answer == some_other_variable  OR
     if @answer[0] == some_other_variable  OR
     if answer_var == some_other_variable

what value do @answer[0] and @answer[0] hold and how can I print the value to the log file? not the web page. I know it must be simple, but I can't get my head around it.

Thanks.

Upvotes: 2

Views: 763

Answers (1)

Thomas Guillory
Thomas Guillory

Reputation: 5729

It's not really an answer to your question but...

If you want to follow "the rails way", you should better use Models and not deal with SQL at all.

E.g. :

@answer = q.answers.first # answers is an array, take the first
if @answer.name == ...

For the logging, I suggest you that : http://guides.rubyonrails.org/debugging_rails_applications.html#the-logger

Upvotes: 1

Related Questions