# Methods in models dont work for views?

**URL:** <https://www.ruby-forum.com/t/methods-in-models-dont-work-for-views/121554>\
**Category:** Rails\
**Created:** [November 28, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/methods-in-models-dont-work-for-views/121554 "2007-11-28T22:02:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 28, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/methods-in-models-dont-work-for-views/121554/1 "2007-11-28T22:02:39Z")

</div>

This question is more towards understanding how things work rather than  
trying to just get something to work.

I have something like this in my user model.

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

This works find if I use it for display values in a  
collection\_for\_select.

options\_from\_collection\_for\_select( @users, :id, :full\_name)

It displays the full name in the selection box options.

But if I try to go \<%=h @user.full\_name %\> I get an error saying

“private method `full\_name’ called for \<…\>”

Do I need write a duplicate method in the user controller?

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

What goes on with the name space issue if I made the two slightly  
different?

Would the collection\_for\_select start looking at the controller action  
instead?

I suppose I don’t really have a problem but duplicating the little  
method strikes me as a bit slovenly. Would putting it in the helper  
area change anything?

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 28, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/methods-in-models-dont-work-for-views/121554/2 "2007-11-28T22:21:02Z")

</div>

On 28 Nov 2007, at 21:02, Tom N. wrote:

> This works find if I use it for display values in a  
> collection\_for\_select.
> 
> options\_from\_collection\_for\_select( @users, :id, :full\_name)

that probably uses send to call the method (which doesn’t care about  
private methods)

> It displays the full name in the selection box options.
> 
> But if I try to go \<%=h @user.full\_name %\> I get an error saying
> 
> “private method `full\_name’ called for \<…\>”

Can I ask the obvious question: is the full\_name method private?

Fred

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 28, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/methods-in-models-dont-work-for-views/121554/3 "2007-11-28T23:02:14Z")

</div>

> that probably uses send to call the method (which doesn’t care about  
> private methods)

maybe i don’t want that code in the model even though it works?

> Can I ask the obvious question: is the full\_name method private?
> 
> Fred

No it will work if I made a controller method.

I was wondering if I really needed a duplicate full\_name method in the  
controller if I had one in the model. Perhaps I should just scrap the  
one in the model and just use one in the controller? Seems a shame to  
have two of them. Would you put that sort of thing in the helper file?

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 28, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/methods-in-models-dont-work-for-views/121554/4 "2007-11-28T23:46:05Z")

</div>

William P. wrote:

> No, you need to look at Fred’s question. That method is simply an  
> attribute method and should not be protected or private. If it is not,  
> it should work fine anywhere your object exists.
> 
> -Bill

Thank you both for staying with me.

I had an error in my model and had my method AFTER the “end” to the  
class. (the end was after the validations then I put the method below  
that! )

I am sorry to waste your time but it was an important thing I was on the  
verge of getting quite wrong. Now that it works, it looks like the model  
is a fine place to put model object specific little data manipulations  
things like that. I was on the verge of loading them into the wrong  
places.

Thanks again for forcing me to take the 10th pass that caught my  
mistake.

Tom

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 28, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/methods-in-models-dont-work-for-views/121554/5 "2007-11-28T23:28:25Z")

</div>

No, you need to look at Fred’s question. That method is simply an  
attribute method and should not be protected or private. If it is not,  
it should work fine anywhere your object exists.

-Bill

Tom N. wrote:

> > Fred
> 
> No it will work if I made a controller method.
> 
> I was wondering if I really needed a duplicate full\_name method in the  
> controller if I had one in the model. Perhaps I should just scrap the  
> one in the model and just use one in the controller? Seems a shame to  
> have two of them. Would you put that sort of thing in the helper file?

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 28, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/methods-in-models-dont-work-for-views/121554/6 "2007-11-28T23:56:30Z")

</div>

No problem, you aren’t wasting my time. We all overlook things from time  
to time. As far as where to put this, you are correct. Anything that  
changes the interface of the model class should go into the model, or a  
module if it’s going to be mixed in to multiple models.

-Bill

Tom N. wrote:

> Thanks again for forcing me to take the 10th pass that caught my  
> mistake.
> 
> Tom

–  
Sincerely,

William P.
