Hussein.
Hussein.

Reputation: 179

undefined method `<<' for #<Answer::ActiveRecord_Relation:0x007fada31c7430>

Hi I create a controller Game to display a Q/A game

And I am blocked with <<, here is the code

def play
    lvlup(lvl)
    if lvl == 1
      set_questions
    else
      get_questions
    end
    @answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")
    @answer ||= []
    @answers << question.answer
    @answers = @answers.shuffle
    render 'play'
  end

I create an array and I put the related answer in the global answers I want to display 4 Max.

Why does the undefined is here?

Here is the total code

class GamesController < ApplicationController
attr_accessor :lvl

  def welcome
  end

  def congrat
  end

  def play
    lvlup(lvl)
    if lvl == 1
      set_questions
    else
      get_questions
    end
    @answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")
    @answer ||= []
    @answers << question.answer
    @answers = @answers.shuffle
    render 'play'
  end

  def loose
      @question = Question.find(params[:question])
      flash.now[:alert] = "Miss..."
      render 'loose'
  end

  def check
    @lvl = params[:lvl].to_i
    answer_id = params[:id].to_i
    question = Question.find(params[:question])
    if @lvl == lvlmax
      render action: 'congrat' and return
    elsif answer_id == question.answer_id
      flash.now[:notice] = "Well done !"
      play
    else answer_id != question.answer_id
      loose
    end
  end

  private

  def lvlup(value)
    @lvl = 1 + value.to_i
  end
  def lvlmax
    @lvlmax = Question.all.count
  end

  def set_questions
    @questionsids = []
    Question.all.shuffle.each do |d|
      @questionsids << d.id
    end
    cookies[:questions] = @questionsids
  end

  def get_questions
    @questions = cookies[:questions].split('&')
  end

  def questions
    @questions = cookies[:questions]
  end

  def question
    @question = Question.find(questions[lvl])
  end

end

Thank you for your help.

Upvotes: 0

Views: 519

Answers (2)

shivam
shivam

Reputation: 16506

mtrolle's answer might be correct, but I have my doubts as to why ActiveRecord::Relation was not returned as Array by default. (Also as mentioned by BroiStatse in his comment.)

I too noticed the same problem with my local setup however it was attributed to another issue all together. I am sharing this here in case you too happen to use MySQL.

Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")

returns an ActiveRecord::Relation object. And it translates to following SQL:

SELECT `answers`.* FROM `answers` WHERE (id != ID) ORDER BY RANDOM() LIMIT 2

When I try running the same in MySQL, I get:

ERROR 1305 (42000): FUNCTION database.RANDOM does not exist

Apparently MySQL does not have RANDOM() function, instead it uses RAND().

Converting ActiveRecord query accordingly returned correct Array to me:

Answer.where.not(id: question.answer_id).limit(2).order("RAND()")

Upvotes: 0

mtrolle
mtrolle

Reputation: 2284

You are trying to append to the @answers result - this is an ActiveRecord relation, you cannot append data to that array.

Add .to_a in the end of your line where you set @answers to allow you to append to the array.

 @answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()").to_a

Upvotes: 2

Related Questions