title: Ruby/Rails - Add records to an object with each loop iteration / Object vs Arrays
body: <p>I'm trying to figure out how to add records to an existing object for each iteration of a loop.  I'm having a hard time discovering the difference between an object and an array.</p>
  
  <p>I have this</p>
  
  <pre><code> @events = Event.find(1)
   @loops = Choices.find(:all, :limit =&gt; 5)  #so loop for 5 instances of choice model
   for loop in @loops
         @events = Event.find(:all,:conditions =&gt; ["event.id = ?", loop.event_id ])
   end
  </code></pre>
  
  <p>I'm trying to add a new events to the existing @events object based on the id of whatever the loop variable is. But the ( = ) operator just creates a new instance of the @events object.</p>
  
  <p>I tried ( += ) and  ( &lt;&lt; ) as operators but got the error </p>
  
  <p>"You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil"</p>
  
  <p>I tried created an array</p>
  
  <pre><code> events = []
   events &lt;&lt; Event.find(1)
   @loops = Choices.find(:all, :limit =&gt; 5)  #so loop for 5 instances of choice model
   for loop in @loops
         events &lt;&lt; Event.find(:all,:conditions =&gt; ["event.id = ?", loop.event_id ])
   end
  </code></pre>
  
  <p>But I dont know how to call that arrays attributes within the view </p>
  
  <p>With objects I was able do create a loop within the view and call all the attributes of that object as well...</p>
  
  <pre><code>&lt;table&gt; 
  &lt;% for event in @events %&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;%= link_to event.title, event %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= event.start_date %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= event.price %&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;% end %&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>How could i do this with an array set?</p>
  
  <p>So the questions are    </p>
  
  <p>1) Whats the difference between arrays and objects?<br>
  2) Is there a way to add into the existing object for each iteration?<br>
  3) If I use an array, is there a way to call the attributes for each array record within the view?</p>