Methods in models dont work for views?

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?

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

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?

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

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.

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.