# Iterating through an array of calls to which a login belongs to

**URL:** <https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996>\
**Category:** Rails\
**Created:** [February 13, 2008, 11:43am UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996 "2008-02-13T11:43:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 13, 2008, 11:43am UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996/1 "2008-02-13T11:43:18Z")

</div>

There’s an array of calls in the controller to which this login belongs  
to. The desired output is the comment attribute for these Call  
instances.

I’m getting an error of:

undefined local variable or method `calls’

which is perhaps due to a syntax or other erb error in the show:

thufir@arrakis ~/goodfellow-tool $ cat app/controllers/  
logins\_controller.rb -n | head -n 18 | tail -n 4  
15 def show  
16 @login = Login.find(params[:id])  
17 calls = @login.calls  
18 end  
thufir@arrakis ~/goodfellow-tool $ cat app/views/logins/show.rhtml -n |  
head -n 25 | tail -n 3  
23 \<% calls.each do |call| %\>  
24 \<%=h @call.comment %\>  
25 \<% end %\>  
thufir@arrakis ~/goodfellow-tool $ cat app/models/call.rb  
class Call \< ActiveRecord::Base  
belongs\_to :login  
end  
thufir@arrakis ~/goodfellow-tool $ cat app/models/login.rb  
class Login \< ActiveRecord::Base  
belongs\_to :employee  
has\_many :calls  
end  
thufir@arrakis ~/goodfellow-tool $

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 13, 2008, 11:57am UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996/2 "2008-02-13T11:57:37Z")

</div>

Thufir wrote:

> thufir@arrakis ~/goodfellow-tool $ cat app/controllers/  
> logins\_controller.rb -n | head -n 18 | tail -n 4  
> 15 def show  
> 16 @login = Login.find(params[:id])  
> 17 calls = @login.calls  
> 18 end  
> thufir@arrakis ~/goodfellow-tool $ cat app/views/logins/show.rhtml -n |  
> head -n 25 | tail -n 3  
> 23 \<% calls.each do |call| %\>  
> 24 \<%=h @call.comment %\>  
> 25 \<% end %\>  
> thufir@arrakis ~/goodfellow-tool $ cat app/models/call.rb  
> class Call \< ActiveRecord::Base  
> belongs\_to :login  
> end  
> thufir@arrakis ~/goodfellow-tool $ cat app/models/login.rb  
> class Login \< ActiveRecord::Base  
> belongs\_to :employee  
> has\_many :calls  
> end  
> thufir@arrakis ~/goodfellow-tool $

Hi Thufir,

You are missing the @ symbol on line 17 in your logins\_controller. it  
should be  
@calls = @login.calls

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 13, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996/3 "2008-02-13T13:07:06Z")

</div>

On 13 Feb 2008, at 10:57, Aditya S. wrote:

> > head -n 25 | tail -n 3  
> > has\_many :calls  
> > end  
> > thufir@arrakis ~/goodfellow-tool $
> 
> Hi Thufir,
> 
> You are missing the @ symbol on line 17 in your logins\_controller. it  
> should be  
> @calls = @login.calls

And show should be  
\<% @calls.each do |call| %\>  
\<%=h call.comment %\>  
\<% end %\>

As it is, you’re iterating over something that doesn’t exist, and on  
top of that the block was yielding you one value but you were using  
another.

Fred

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 13, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996/4 "2008-02-13T18:53:04Z")

</div>

On Wed, 13 Feb 2008 12:06:05 +0000, Frederick C. wrote:

> And show should be

Thank you 🙂

Is it ok (the convention) to put that   
 in there? I need to add a  
title, and start putting things into nice columns and stuff, but one  
step  
at a time 🙂

thufir@arrakis ~/goodfellow-tool $ cat app/views/logins/show.rhtml -n |  
head -n 26 | tail -n 4  
23 \<% @calls.each do |call| %\>  
24 \<%=h call.comment %\>  
25   
  
26 \<% end %\>  
thufir@arrakis ~/goodfellow-tool $

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 13, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996/5 "2008-02-13T19:00:44Z")

</div>

On Wed, 13 Feb 2008 11:57:37 +0100, Aditya S. wrote:

> You are missing the @ symbol on line 17 in your logins\_controller. it  
> should be  
> @calls = @login.calls

Thanks, I corrected it to:

thufir@arrakis ~/goodfellow-tool $ cat app/controllers/  
logins\_controller.rb -n | head -n 18 | tail -n 4  
15 def show  
16 @login = Login.find(params[:id])  
17 @calls = @login.calls  
18 end  
thufir@arrakis ~/goodfellow-tool $

What I don’t understand is that there’s a Call class (model), but  
there’s  
no Calls model. How in the world can there be an instance of a class,  
Calls, which doesn’t exist?

Clearly, class Calls does exist – this must be the magic of rails, what  
rails does for me. I dunno, too easy.

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 14, 2008, 6:46am UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996/6 "2008-02-14T06:46:58Z")

</div>

On Feb 13, 6:06 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:  
[…]

> There is no Calls class. what makes you think here is one ?

When the show uses @shows, doesn’t the “@” indicate instance  
variable? I would think that @shows is an array, and yes it’s an  
object so, in that sense, I guess, is an instance variable of Array.

Why is the “@” required in this case?

-Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 14, 2008, 3:07am UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996/7 "2008-02-14T03:07:11Z")

</div>

On 13 Feb 2008, at 17:55, Thufir wrote:

> logins\_controller.rb -n | head -n 18 | tail -n 4  
> Calls, which doesn’t exist?
> 
> Clearly, class Calls does exist – this must be the magic of rails,  
> what  
> rails does for me. I dunno, too easy.

There is no Calls class. what makes you think here is one ?

Fred

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 14, 2008, 8:10am UTC](https://www.ruby-forum.com/t/iterating-through-an-array-of-calls-to-which-a-login-belongs-to/128996/8 "2008-02-14T08:10:56Z")

</div>

On 14 Feb 2008, at 05:46, Thufir wrote:

> On Feb 13, 6:06 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:  
> […]
> 
> > There is no Calls class. what makes you think here is one ?
> 
> When the show uses @shows, doesn’t the “@” indicate instance  
> variable? I would think that @shows is an array, and yes it’s an  
> object so, in that sense, I guess, is an instance variable of Array.

Well yes @calls is an instance variable (of an instance of your  
controller), but that’s got nothing to do with their being or not a  
Calls class. And it’s definitely not an instance variable of Array  
(unless you meant that it’s an instance variable that happens to be an  
array)

> Why is the “@” required in this case?  
> Well if there was no @ then it wouldn’t be an instance variable and so  
> your view wouldn’t be able to play with it

Fred
