# Error of  undefined method

**URL:** <https://discuss.rubyonrails.org/t/error-of-undefined-method/59540>\
**Category:** rubyonrails-talk\
**Created:** [June 20, 2011, 4:33pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540 "2011-06-20T16:33:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 20, 2011, 4:33pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/1 "2011-06-20T16:33:15Z")

</div>

Hi all, I have very basic error that i have no idea how to show

I have user table id name add phone number

then i select all in user table using =\> @users = User.all

=\> @users.name

it gives me undefined method name Array\<.....\>

Please give me some advice..

Thanks

---

<div class="post-metadata">

**Author:** ![Tom\_Meinlschmidt](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@Tom\_Meinlschmidt](https://discuss.rubyonrails.org/u/Tom_Meinlschmidt)\
**Post date:** [June 20, 2011, 4:37pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/2 "2011-06-20T16:37:08Z")

</div>

@users.all basically returns "array" of users

so you have to iterate through this array

@users.each do |user| &nbsp;&nbsp;puts user.name end

tom

---

<div class="post-metadata">

**Author:** ![Everaldo\_Gomes](https://avatars.discourse-cdn.com/v4/letter/e/ea5d25/32.png) [@Everaldo\_Gomes](https://discuss.rubyonrails.org/u/Everaldo_Gomes)\
**Post date:** [June 20, 2011, 4:38pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/3 "2011-06-20T16:38:39Z")

</div>

Hi!

The method User.all returns an Array of users, not a single user.

I think you have to iterate over the array, using each.

@users.each { |u| [u.name](http://u.name) }

Everaldo

---

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 20, 2011, 4:48pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/4 "2011-06-20T16:48:52Z")

</div>

thanks

---

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 20, 2011, 4:53pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/5 "2011-06-20T16:53:50Z")

</div>

I have other undefined method when i combine the code above together

\<%@users = User.all%\> \<%@users.each do |u|%\> \<%u.culture\_id%\>

\<% @pic= Picture.where(:phrase\_id =\> :route , :culture\_id =\> u.culture\_id).first%\> My picture \<%= @pic.image%\>

and it causes undefined method image

please give me advices… thank you so much

Joanne

> thanks

> > Hi!
> > 
> > The method User.all returns an Array of users, not a single user.
> > 
> > I think you have to iterate over the array, using each.
> > 
> > @users.each { |u| [u.name](http://u.name) }
> > 
> > Everaldo

> > > Hi all, I have very basic error that i have no idea how to show
> > > 
> > > I have user table
> > > 
> > > id
> > > 
> > > name
> > > 
> > > add
> > > 
> > > phone number
> > > 
> > > then i select all in user table
> > > 
> > > using =\> @users = User.all
> > > 
> > > =\> @[users.name](http://users.name)
> > > 
> > > it gives me undefined method name Array\<…\>
> > > 
> > > Please give me some advice…
> > > 
> > > Thanks

Joanne

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 20, 2011, 6:42pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/6 "2011-06-20T18:42:14Z")

</div>

What does it say has not got a method image? It is always a good idea to post the complete error message (use copy/paste rather than re-typing it). If it says the nil has not got the method then your call of Picture.where has not found a record.

Colin

---

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 20, 2011, 7:08pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/7 "2011-06-20T19:08:32Z")

</div>

the error is undefined method `image’ for nil:NilClass

---

<div class="post-metadata">

**Author:** ![Chris\_Kottom1](https://avatars.discourse-cdn.com/v4/letter/c/c77e96/32.png) [@Chris\_Kottom1](https://discuss.rubyonrails.org/u/Chris_Kottom1)\
**Post date:** [June 20, 2011, 7:33pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/8 "2011-06-20T19:33:13Z")

</div>

You don’t have an \<% end %\> to close the \<% @users.each %\> block in the code you included, so my guess is that your u variable is already out of scope by the time you call u.image.

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [June 21, 2011, 7:23am UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/9 "2011-06-21T07:23:16Z")

</div>

Please don't top post, it makes it difficult to follow the thread. Insert your reply at appropriate places in previous message. Thanks.

> the error is undefined method `image' for nil:NilClass

So what does that mean (see my previous post)?

Colin

---

<div class="post-metadata">

**Author:** ![Chirag](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Chirag](https://discuss.rubyonrails.org/u/Chirag)\
**Post date:** [June 21, 2011, 8:11am UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/10 "2011-06-21T08:11:55Z")

</div>

Hi,

Few things here:

1. Would be good if you can paste full code from the beginning of the loop till the place where you render picture

2. It is not a good practice to set instance variables in the views, set them in controller instead

3. It is not a good practice to do finds in views, if you want to fetch a picture for the user, it’s better to define a relationship between picture and user models. And then do something like this in the controller - @users = User.includes(:picture).all

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 21, 2011, 1:47pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/11 "2011-06-21T13:47:26Z")

</div>

My controller:

def index @users= User.all

```
@users.each do |p|   
@pic= Picture.where(:phrase_id => :route , :culture_id => p.culture_id).first
 send_data @pic.image, :type => 'image/gif', :disposition => 'inline'
end

```

end

in View

 \<%= image\_tag url\_for(:controller =\> "/users", :action =\> "index"), :width =\> "25px", :height =\> "25px"%\>

\<%@pic.each do |c|%\> \<%= c.image%\> \<% end %\>

please give me some advices…

thanks Joanne

---

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 21, 2011, 1:52pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/12 "2011-06-21T13:52:23Z")

</div>

> My controller:
> 
> def index @users= User.all
> 
> ```
> @users.each do |p|   
> 
> @pic= Picture.where(:phrase_id => :route , :culture_id => p.culture_id).first
> send_data @pic.image, :type => 'image/gif', :disposition => 'inline'
> 
> end
> 
> ```
> 
> end
> 
> in View
> 
> \<%= image\_tag url\_for(:controller =\> "/users", :action =\> "index"), :width =\> "25px", :height =\> "25px"%\>
> 
> \<%@pic.each do |c|%\> \<%= c.image%\> \<% end %\>
> 
> error is 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.each

Extracted source (around line #79):

76: 77: \<%= image\_tag url\_for(:controller =\> “/users”, :action =\> “index”), :width =\> “25px”, :height =\> “25px”%\>

78: 79: \<%@pic.each do |c|%\> 80: \<%= c.image%\> 81: \<% end %\> 82:

---

<div class="post-metadata">

**Author:** ![Chirag](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Chirag](https://discuss.rubyonrails.org/u/Chirag)\
**Post date:** [June 21, 2011, 1:57pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/13 "2011-06-21T13:57:50Z")

</div>

Your index action should be something like this:

def index

@users= User.all

@pic =

@users.each do |p|

@pic \<\< Picture.where(:phrase\_id =\> :route , :culture\_id =\> p.culture\_id).limit(1)

end

end

---

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 21, 2011, 2:12pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/14 "2011-06-21T14:12:29Z")

</div>

it does not work, it is complaining other error

NoMethodError (undefined method `image' for [nil]:Array): app/controllers/patients_controller.rb:27:in `block in index’ app/controllers/patients\_controller.rb:25:in `each' app/controllers/patients_controller.rb:25:in `index’

---

<div class="post-metadata">

**Author:** ![Chirag](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Chirag](https://discuss.rubyonrails.org/u/Chirag)\
**Post date:** [June 21, 2011, 2:27pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/15 "2011-06-21T14:27:14Z")

</div>

Oh yes… sorry about that.

I assumed that you will find picture for every query.

You can try this instead:

def index

@users= User.all

@pic =

@users.each do |p|

@pic \<\< Picture.where(:phrase\_id =\> :route , :culture\_id =\> p.culture\_id).limit(1)

end

@pic.compact!

end

by calling compact! on the array, we will eliminate all nil object.

On another note, are you sure, this is what you want to do? If you have 100 users, it will fire 100 sql queries which is not good.

---

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 21, 2011, 2:40pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/16 "2011-06-21T14:40:17Z")

</div>

> Oh yes… sorry about that. I assumed that you will find picture for every query.

> 

> You can try this instead:

> def index

> @users= User.all

> @pic =

> @users.each do |p|

> @pic \<\< Picture.where(:phrase\_id =\> :route , :culture\_id =\> p.culture\_id).limit(1)

> end

> @pic.compact!

> end

> 

> by calling compact! on the array, we will eliminate all nil object.

> 

> On another note, are you sure, this is what you want to do? If you have 100 users, it will fire 100 sql queries which is not good.

> 

Yes i think i have that problem as well because if i remove "@users.each do |p| "

and then it will cause undefined method of culture\_id… plus when i call @pic.compact! , it gives me

NoMethodError (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.compact!): app/controllers/patients\_controller.rb:29:in `index’

what is mean? cuz i want to display picture on the browser too…

please help… thank you very much

Joanne

---

<div class="post-metadata">

**Author:** ![Chirag](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Chirag](https://discuss.rubyonrails.org/u/Chirag)\
**Post date:** [June 21, 2011, 2:56pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/17 "2011-06-21T14:56:58Z")

</div>

Can you paste what you have in your index method?

You should not be getting this error because we have already defined @pic as an empty array.

---

<div class="post-metadata">

**Author:** ![Google\_Mail](https://avatars.discourse-cdn.com/v4/letter/g/4491bb/32.png) [@Google\_Mail](https://discuss.rubyonrails.org/u/Google_Mail)\
**Post date:** [June 21, 2011, 3:05pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/18 "2011-06-21T15:05:21Z")

</div>

> Can you paste what you have in your index method? You should not be getting this error because we have already defined @pic as an empty array.

> def index @users= User.all @pic=
> 
> ```
> @users.each do |p|   
> @pic<< Picture.where(:phrase_id => :route , :culture_id => p.culture_id).first
> 
> end
> 
> @pic.compact!
> 
> send_data @pic.image, :type => 'image/png', :disposition => 'inline'
> 
> ```
> 
> end
> 
> > > Oh yes… sorry about that. I assumed that you will find picture for every query.

> > >

> > > You can try this instead:

> > > def index

> > > @users= User.all

> > > @pic =

> > > @users.each do |p|

> > > @pic \<\< Picture.where(:phrase\_id =\> :route , :culture\_id =\> p.culture\_id).limit(1)

> > > end

> > > @pic.compact!

> > > end

> > >

> > > by calling compact! on the array, we will eliminate all nil object.

> > >

> > > On another note, are you sure, this is what you want to do? If you have 100 users, it will fire 100 sql queries which is not good.

> > >

> > Yes i think i have that problem as well because if i remove "@users.each do |p| "
> > 
> > and then it will cause undefined method of culture\_id… plus when i call @pic.compact! , it gives me
> > 
> > NoMethodError (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.compact!): app/controllers/patients\_controller.rb:29:in `index’
> > 
> > what is mean? cuz i want to display picture on the browser too…
> > 
> > please help… thank you very much
> > 
> > Joanne

> > > > it does not work, it is complaining other error
> > > > 
> > > > NoMethodError (undefined method `image' for [nil]:Array): app/controllers/patients_controller.rb:27:in `block in index’ app/controllers/patients\_controller.rb:25:in `each’
> > > > 
> > > > app/controllers/patients\_controller.rb:25:in `index’

> > > > > Your index action should be something like this:

> > > > >

> > > > > def index

> > > > > @users= User.all

> > > > > @pic =

> > > > > @users.each do |p|

> > > > > @pic \<\< Picture.where(:phrase\_id =\> :route , :culture\_id =\> p.culture\_id).limit(1)

> > > > > end

> > > > > end

> > > > > –
> > > > > 
> > > > > You received this message because you are subscribed to the Google Groups “Ruby on Rails: Talk” group.

> > > > > To view this discussion on the web visit [https://groups.google.com/d/msg/rubyonrails-talk/-/B259TKI0970J](https://groups.google.com/d/msg/rubyonrails-talk/-/B259TKI0970J).

> > > > > To post to this group, send email to [rubyonrails-talk@googlegroups.com](mailto:rubyonrails-talk@googlegroups.com).
> > > > > 
> > > > > To unsubscribe from this group, send email to [rubyonrails-talk+unsubscribe@googlegroups.com](mailto:rubyonrails-talk+unsubscribe@googlegroups.com).
> > > > > 
> > > > > For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

> > > >

> > > > ## –
> > > > 
> > > > Thank you,
> > > > 
> > > > Yen

> > > > –
> > > > 
> > > > You received this message because you are subscribed to the Google Groups “Ruby on Rails: Talk” group.

> > > > To post to this group, send email to [rubyonrails-talk@googlegroups.com](mailto:rubyonrails-talk@googlegroups.com).
> > > > 
> > > > To unsubscribe from this group, send email to [rubyonrails-talk+unsubscribe@googlegroups.com](mailto:rubyonrails-talk+unsubscribe@googlegroups.com).
> > > > 
> > > > For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

> > > –

> > > Chirag [http://sumeruonrails.com](http://sumeruonrails.com)

> > >

> > > –
> > > 
> > > You received this message because you are subscribed to the Google Groups “Ruby on Rails: Talk” group.

> > > To post to this group, send email to [rubyonrails-talk@googlegroups.com](mailto:rubyonrails-talk@googlegroups.com).
> > > 
> > > To unsubscribe from this group, send email to [rubyonrails-talk+unsubscribe@googlegroups.com](mailto:rubyonrails-talk+unsubscribe@googlegroups.com).
> > > 
> > > For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

> >

> > –
> > 
> > You received this message because you are subscribed to the Google Groups “Ruby on Rails: Talk” group.
> > 
> > To post to this group, send email to [rubyonrails-talk@googlegroups.com](mailto:rubyonrails-talk@googlegroups.com).
> > 
> > To unsubscribe from this group, send email to [rubyonrails-talk+unsubscribe@googlegroups.com](mailto:rubyonrails-talk+unsubscribe@googlegroups.com).
> > 
> > For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

> – Chirag [http://sumeruonrails.com](http://sumeruonrails.com)

> 

> –
> 
> You received this message because you are subscribed to the Google Groups “Ruby on Rails: Talk” group.
> 
> To post to this group, send email to [rubyonrails-talk@googlegroups.com](mailto:rubyonrails-talk@googlegroups.com).
> 
> To unsubscribe from this group, send email to [rubyonrails-talk+unsubscribe@googlegroups.com](mailto:rubyonrails-talk+unsubscribe@googlegroups.com).
> 
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![hassan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/hassan/32/714_2.png) [@hassan](https://discuss.rubyonrails.org/u/hassan)\
**Post date:** [June 21, 2011, 3:25pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/19 "2011-06-21T15:25:09Z")

</div>

You would probably have fewer problems if you observed the Rails naming conventions; e.g.

@pictures = # plural

@users.each do |user| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pictures \<\< Picture.where(:phrase\_id =\> :route , :culture\_id =\>user.culture\_id).first end

@pictures.compact!

Now it's obvious that the next line makes no sense:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; send\_data @pictures.image, :type =\> 'image/png', :disposition =\> 'inline'

An \*array\* of pictures doesn't have an "image" attribute.

HTH,

---

<div class="post-metadata">

**Author:** ![Chirag](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@Chirag](https://discuss.rubyonrails.org/u/Chirag)\
**Post date:** [June 21, 2011, 3:28pm UTC](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540/20 "2011-06-21T15:28:40Z")

</div>

1. Remove the send\_data line, it isn’t required.

2. Can you check if there are any records in your pictures table? Right now it looks like it is returning nil.

[Next page](https://discuss.rubyonrails.org/t/error-of-undefined-method/59540.md?page=2)
