Topic: Issue looping

Hi this is the gist of my code

  def gen_questions(*qn_array)
    if !questions.nil?
      qn_array.each do |i|
      self.questions.build(:question_no=>i,:answer=>"",:score=>0)
      self.save
      end        
    end  
  end

The problem with this code is that only 1 question is created.
How to change it in such as way that the question is created the same number of time as the number of elements in the array?
Thanks.

Re: Issue looping

qn_array.each do |i|
  #building 3 questions, you can build any number you want
    3.times {self.questions.build(:question_no=>i,:answer=>"",:score=>0) }    
    self.save
end   

Last edited by Javix (2012-06-16 08:30:44)

Re: Issue looping

limgw wrote:

The problem with this code is that only 1 question is created.

Out of curiosity, what's the size of the array?  If its 1, then the following (based off of Javix's post) isn't going to fix anything:

qn_array.length.times { self.questions.build(:question_no=>i,:answer=>"",:score=>0) }
self.save

Just realized that you do actually need the for-each loop.  So never mind the above piece of code.

Last edited by ToddG (2012-06-16 22:53:05)

Re: Issue looping

The array is contains the id of the list of generated questions.
The issue with the code is that when I view the database only 1 entry is generated.
The array definitely contains more than 1 elements
Even if I feed the method (using rails console)
@s.gen_questions([1,2,3,4,5,6,7,8,9])
@s.save
It still generates 1 question only.
Update: Using Javix code creates the number of entries correctly but the question number is not the value of each element in the array, but I need the number of entries created to be dynamically determined by the number of elements in the array.
eg @array contains [123,456,446,8,2,6,7,32]
So the number of entries to be created is 8 and each time question number is 123 for the first entry, 456 for the second entry and so on.

Last edited by limgw (2012-06-16 22:38:45)

Re: Issue looping

I tried another approach by declaring the array inside the function and my original method works!?
So the problem is somehow the array is not passed properly as a parameter.
Is this part of a design?
I can work around it by calling the array generator inside the function but it wouldn't be nice.

Re: Issue looping

Have you tried adding a puts to verify that the for-each loop is behaving the way you think it should?

 def gen_questions(*qn_array)
    if !questions.nil?
      qn_array.each do |i|
      self.questions.build(:question_no=>i,:answer=>"",:score=>0)
      puts “:question_no => #{i}, :answer => \”\”, :score => 0”
      self.save
      end        
    end  
  end

It might have something to do with how your Model is setup. (ie. validates_uniqueness_of)

Re: Issue looping

ToddG wrote:

Have you tried adding a puts to verify that the for-each loop is behaving the way you think it should?

 def gen_questions(*qn_array)
    if !questions.nil?
      qn_array.each do |i|
      self.questions.build(:question_no=>i,:answer=>"",:score=>0)
      puts “:question_no => #{i}, :answer => \”\”, :score => 0”
      self.save
      end        
    end  
  end

It might have something to do with how your Model is setup. (ie. validates_uniqueness_of)

I tested by calling the array generator inside the function and then using that @array to create the questions it works as expected, it just fails when the array was passed as a parameter.
I can live with that it is just that the whole thing is strange.
My model doesn't have any validation

Last edited by limgw (2012-06-16 23:45:41)

Re: Issue looping

I don't know then.  Sorry.

Re: Issue looping

Hello!
Please help!
Why cant I use the