# Cannot access an objectâ€™s attributes in an array

**URL:** <https://www.ruby-forum.com/t/cannot-access-an-objecta-s-attributes-in-an-array/173034>\
**Category:** Rails\
**Created:** [July 31, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/cannot-access-an-objecta-s-attributes-in-an-array/173034 "2009-07-31T19:25:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fahrio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e68b1a/32.png) [@fahrio](https://www.ruby-forum.com/u/fahrio)\
**Post date:** [July 31, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/cannot-access-an-objecta-s-attributes-in-an-array/173034/1 "2009-07-31T19:25:04Z")

</div>

Hello,

I am building a site with works and their credits. What I am trying o  
achieve is to find each work’s similar works based on the mutual  
titles in their credits.

I am adding each similar work into an array in a for loop and when I  
try to access the attributes of these works I get a “nil object when  
you didn’t expect it!” error. I can see the Work objects when I debug  
in the array, but can’t access to their attributes. Here is the code:

class Work \< ActiveRecord::Base

def similar\_works  
@similar\_works  
end

def find\_similar\_works  
@similar\_works = []  
for credit in self.credits  
same\_credits = credit.title.credits #same credits with mutual  
titles  
for credit2 in same\_credits  
@similar\_works \<\< credit2.work  
end  
end  
end

end

class WorksController \< ApplicationController

def index  
list  
render(:action =\> ‘list’)  
end

def list  
# find similar works for each work  
@works.each do |work|  
work.find\_similar\_works  
end  
end

end

list.html

\<% for work in @works -%\>  
\<% for similarwork in work.similar\_works%\>  
\<%= similarwork.name%\> =\> nil object  
\<%=debug(similarwork)%\> =\> sample debug output is below  
\<% end %\>  
\<% end %\>

— !ruby/object:Work  
attributes:  
name: Borozan  
updated\_at: 2009-07-31 12:30:30  
created\_at: 2009-07-31 12:25:32  
attributes\_cache: {}

— !ruby/object:Work  
attributes:  
name: Boom  
updated\_at: 2009-07-31 12:30:30  
created\_at: 2009-07-31 12:25:32  
attributes\_cache: {}

— !ruby/object:Work  
attributes:  
name: Kamuflaj  
updated\_at: 2009-07-31 12:30:30  
created\_at: 2009-07-31 12:25:32  
attributes\_cache: {}

---

<div class="post-metadata">

**Author:** ![fahrio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e68b1a/32.png) [@fahrio](https://www.ruby-forum.com/u/fahrio)\
**Post date:** [July 31, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/cannot-access-an-objecta-s-attributes-in-an-array/173034/2 "2009-07-31T21:27:59Z")

</div>

2009/7/31 fahrio [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> in the array, but can’t access to their attributes. Here is the code:  
> Â Â for credit in self.credits
> 
> Â Â @works.each do |work|  
> \<% for work in @works -%\>  
> \<% for similarwork in work.similar\_works%\>  
> Â Â \<%= similarwork.name%\> =\> nil object  
> Â Â \<%=debug(similarwork)%\> =\> sample debug output is below  
> \<% end %\>  
> \<% end %\>

Try putting the debug(similarwork) line before the similarwork.name  
line. As you have it you will see the debug for all the good ones,  
then the first nil one will cause the error and will not get to the  
debug. That is a possibility anyway.  
Also you could use ruby-debug to break in at that point and inspect  
the whole set.

Colin

---

<div class="post-metadata">

**Author:** ![fahrio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e68b1a/32.png) [@fahrio](https://www.ruby-forum.com/u/fahrio)\
**Post date:** [July 31, 2009, 10:21pm UTC](https://www.ruby-forum.com/t/cannot-access-an-objecta-s-attributes-in-an-array/173034/3 "2009-07-31T22:21:50Z")

</div>

Can you give us a bit more info about what “credit.title.credits”  
looks like? It looks like it should be an array of objects. Put a  
logger.info similar\_works.inspect and look at your development.log to  
see what it says about the values it ends up with.

If it isn’t an array or if it is an empty array you will just end up  
doing nothing and that function just exits without accomplishing much.

---

<div class="post-metadata">

**Author:** ![fahrio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e68b1a/32.png) [@fahrio](https://www.ruby-forum.com/u/fahrio)\
**Post date:** [July 31, 2009, 10:26pm UTC](https://www.ruby-forum.com/t/cannot-access-an-objecta-s-attributes-in-an-array/173034/4 "2009-07-31T22:26:30Z")

</div>

I see I have used the wrong variable name here… Instead of  
similar\_works, use logger.info same\_credits.inspect

Sorry about that. Your code snippet had quite a few  
similar\_variable\_names. 😛
