Trent Scott
Trent Scott

Reputation: 2028

Rails Pluralize Not Working When Controller Fetches One Object?

I've been doing some testing in a simple Rails app.

In my controller, I assign an instance variable like this:

@user = User.last

This is strictly for testing — I have no good reason to pull the last user in this way.

In my view, I use the pluralize method to report the number of users. Of course, since I'm using User.last, there is only one.

In the view, the code is <%= pluralize(@user.size, 'User') %>.

When it runs the application throws a NoMethodError for size.

You would think that size would report 1 in this case but, for some reason, it doesn't.

I'm curious why?

Upvotes: 0

Views: 61

Answers (1)

Billy Chan
Billy Chan

Reputation: 24815

The title is a false claim. pluralize is just a helper, it doesn't matter where you use it.

In your case, @user is a single record. It doesn't have size method. That's true. So the error is expected, nothing wrong.

Upvotes: 1

Related Questions