# String attributes not showing up in view

**URL:** <https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806>\
**Category:** Rails\
**Created:** [June 8, 2010, 6:20am UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806 "2010-06-08T06:20:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 8, 2010, 6:20am UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/1 "2010-06-08T06:20:00Z")

</div>

Hi,

I have a user model with attributes first\_name, last\_name and email.

In the view I’m looping though all the users and for each user  
printing out the email add the full\_name (full\_name is a function in  
the user model that returns both first and last names). The email  
prints but the names do not and it’s really weird. I have checked in  
the script/console and verified that the values are actually inside  
the user object (I only have one user object created with the  
migration).

What’s even weirder is that when I call User.find\_by\_email “blah” and  
it gives me back my user, the output in the console shows all the  
fields, shows the first\_name and last\_name values as well. But in the  
console if I do u.first\_name, I get a nil return. If I do u.email, I  
get the “blah” return. Any idea where I can start looking for a  
problem?

In the user.index.html.erb

\<% @users.each do |user| %\>

 \<%= user.full\_name %\> \<%= user.email %\> \<% end %\>

and in my User model:

def full\_name  
@first\_name + " " + @last\_name  
end

Thanks in advance

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 8, 2010, 6:56am UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/2 "2010-06-08T06:56:53Z")

</div>

what’s with the @first\_name, @last\_name?

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 8, 2010, 7:27am UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/3 "2010-06-08T07:27:54Z")

</div>

What do you mean? What’s wrong with them?

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 8, 2010, 9:12am UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/4 "2010-06-08T09:12:04Z")

</div>

On 8 June 2010 05:18, Ali [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and in my User model:
> 
> def full\_name  
> @first\_name + " " + @last\_name  
> end

Parameters of a model aren’t assigned to instance variables, they’re  
accessed through method calls. So drop the “@” signs. While you’re at  
it, you can get rid of the string concatenation:

def full\_name  
[first\_name, last\_name].join(" ")  
end

…now, if you’re missing a first or last name, you don’t get extra  
white-space padding (and if you want to include ‘title’ or  
‘middle\_name’ you’re not creating n+1 strings).

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 8, 2010, 10:37am UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/5 "2010-06-08T10:37:34Z")

</div>

On Jun 8, 3:10 am, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def full\_name  
> [first\_name, last\_name].join(" ")  
> end
> 
> …now, if you’re missing a first or last name, you don’t get extra  
> white-space padding (and if you want to include ‘title’ or  
> ‘middle\_name’ you’re not creating n+1 strings).

Never thought of that. Nice tip. Thanks.

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 8, 2010, 2:56pm UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/6 "2010-06-08T14:56:42Z")

</div>

pepe wrote:

> On Jun 8, 3:10ï¿½am, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ï¿½def full\_name  
> > ï¿½ï¿½ ï¿½[first\_name, last\_name].join(" ")  
> > ï¿½ï¿½end
> > 
> > …now, if you’re missing a first or last name, you don’t get extra  
> > white-space padding

Actually, I believe you do, unless you compact the array first (posting  
from iPhone away from irb right now, so can’t verify).

> > (and if you want to include ‘title’ or  
> > ‘middle\_name’ you’re not creating n+1 strings).
> 
> Never thought of that. Nice tip. Thanks.

Indeed. I find myself using String#+ very rarely – join and “#{ }” are  
both more efficient and no harder to type.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 9, 2010, 2:17am UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/7 "2010-06-09T02:17:54Z")

</div>

Oh ok. Did not know that. Thanks!

After I got rid of the ‘@’ part they were still returning nil. I had an  
attr\_accessor :first\_name, :last\_name defined. Things worked after  
getting  
rid of that. I guess having attr\_Accessor with the same names overrides  
the  
method calls or something?

Thanks again!

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 9, 2010, 10:29am UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/8 "2010-06-09T10:29:34Z")

</div>

On Jun 9, 1:16 am, Ali A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Oh ok. Did not know that. Thanks!
> 
> After I got rid of the ‘@’ part they were still returning nil. I had an  
> attr\_accessor :first\_name, :last\_name defined. Things worked after getting  
> rid of that. I guess having attr\_Accessor with the same names overrides the  
> method calls or something?

You don’t need attr\_accessor for attributes from the database -  
attr\_accessor will overwrite (or prevent the generation of) the  
accessors active record provides.

Fred

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 8, 2010, 3:27pm UTC](https://www.ruby-forum.com/t/string-attributes-not-showing-up-in-view/189806/9 "2010-06-08T15:27:26Z")

</div>

On 8 June 2010 13:56, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> from iPhone away from irb right now, so can’t verify).  
> 100% correct… sorry - my real methods do compact before the join…  
> just not when I write them in Gmail! 🙂
