# Undefined method \`name'

**URL:** <https://www.ruby-forum.com/t/undefined-method-name/142541>\
**Category:** Rails\
**Created:** [July 9, 2008, 1:04am UTC](https://www.ruby-forum.com/t/undefined-method-name/142541 "2008-07-09T01:04:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joernborup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joernborup](https://www.ruby-forum.com/u/joernborup)\
**Post date:** [July 9, 2008, 1:04am UTC](https://www.ruby-forum.com/t/undefined-method-name/142541/1 "2008-07-09T01:04:40Z")

</div>

Hi Community…

I’m new in this game, so this might be a easy question, but I have  
done my google, without result, so I will try this…

I’m learning ROR, and I’m using Patrick Lens ‘Ruby on Rails’ book.  
(This is written for ror 1.x - and I’m using NetBeans 6.1 - ror 2.x -  
this might be the problem…)

But…

I have made this Controller:

class StoryController \< ApplicationController  
def index  
@current\_time = Time.now  
@s = Story.find\_all\_by\_name(‘sitepoint’)  
end  
end

and this view:

# Story#index
hej \<%= @current\_time %\> \<%= @s.name%\>

Find me in app/views/story/index.html.erb

And, when browsing my ‘story’ ([http://localhost:3000/story](http://localhost:3000/story)), I get  
this error:  
undefined method `name’ for [#\<Story id: 2, name: “sitepoint”, link:  
“[http://sitepoint](http://sitepoint)”\>]:Array

Now using the Rails console within Netbeans, gives me this

> > v = Story.find\_by\_name(‘sitepoint’)  
> > v = Story.find\_by\_name(‘sitepoint’)  
> > =\> #\<Story id: 2, name: “sitepoint”, link: “[http://sitepoint](http://sitepoint)”\>  
> > v.name  
> > v.name  
> > =\> “sitepoint”

No problem with ‘name’ - so what is the difference ? -

Regards  
Jørn

---

<div class="post-metadata">

**Author:** ![joernborup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joernborup](https://www.ruby-forum.com/u/joernborup)\
**Post date:** [July 9, 2008, 1:26am UTC](https://www.ruby-forum.com/t/undefined-method-name/142541/2 "2008-07-09T01:26:02Z")

</div>

On Jul 8, 2008, at 4:03 PM, jborup wrote:

> But…  
> and this view:  
> “[http://sitepoint](http://sitepoint)”\>]:Array  
> No problem with ‘name’ - so what is the difference ? -

In the first example you are using “find\_all\_by\_name”. In the second  
you are using “find\_by\_name”. The first returns an array. The second  
returns a Story object.

You can see this in the error:

> undefined method `name’ for [#\<Story id: 2, name: “sitepoint”, link:  
> “[http://sitepoint](http://sitepoint)”\>]:Array

So you either need to loop in your view over @s. ie…

\<% @s.each |story| do %\>  
\<%= story.name %\>  
\<% end %\>

Or change your controller to match what you are doing in console.

-philip

---

<div class="post-metadata">

**Author:** ![joernborup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joernborup](https://www.ruby-forum.com/u/joernborup)\
**Post date:** [July 9, 2008, 1:26am UTC](https://www.ruby-forum.com/t/undefined-method-name/142541/3 "2008-07-09T01:26:02Z")

</div>

> I have made this Controller:
> 
> class StoryController \< ApplicationController  
> def index  
> @current\_time = Time.now  
> @s = Story.find\_all\_by\_name(‘sitepoint’)  
> end  
> end  
> \<…\>

> Now using the Rails console within Netbeans, gives me this
> 
> > > v = Story.find\_by\_name(‘sitepoint’)  
> > > v = Story.find\_by\_name(‘sitepoint’)  
> > > =\> #\<Story id: 2, name: “sitepoint”, link: “[http://sitepoint](http://sitepoint)”\>  
> > > v.name  
> > > v.name  
> > > =\> “sitepoint”
> 
> No problem with ‘name’ - so what is the difference ? -

Can you spot a difference between  
Story.find\_all\_by\_name(‘sitepoint’)  
and  
Story.find\_by\_name(‘sitepoint’) ?

Now think, what you get back from the first query, and what you get  
back from the second one.  
Then answer yourself, does Array have an ‘name’ attribute?

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![joernborup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joernborup](https://www.ruby-forum.com/u/joernborup)\
**Post date:** [July 9, 2008, 1:39am UTC](https://www.ruby-forum.com/t/undefined-method-name/142541/4 "2008-07-09T01:39:21Z")

</div>

Yes… hmmm… that is very clear, and removing the all, solved the  
problem… guess it is getting a bit late…

Thank you for you comments… I’m back on track…

Regards  
Jørn.
