views:

105

answers:

2

Hey guys,

I have the following code in an action:

@user = @current_user
@user.votes[1430]='up'
@user.update_attributes(params[:user])

Votes is a string type, there is no default value - I just want to set it for the first time when this action occurs.

Unfortunately I get this error: NoMethodError

You have a nil object when you didn't expect it! The error occurred while evaluating nil.votes

any ideas what I'm doing wrong?

A: 

The cause of the error seems to be that @user is a nil reference.

You can confirm this by using logging and checking in your console window:

logger.info "user is nil" if @user.nil?

You are assigning @user to be the value of @current_user. I've seen this pattern before and usually current_user is a function, declared elsewhere, not an instance variable. If you are using that pattern, the line should be something like @user = current_user instead.

(Additionally, if votes is a string, your second line appears to be referring to index 1430 of that string, which is probably not what you want either.)

Evil Trout
Hi Evil Trout,Thanks for the answer! Doesn't the index 1430 make votes array? Because if so, its what I'm trying to do.Got a similar error from @user = current_user, I'm using authlogic if that makes a difference
Elliot
Are you requiring that the user be logged in before this action? And also, the index on votes, if it is a string, will not make it an array, it will look up the character at position 1430 of your string. Here's some more info on that http://www.ruby-doc.org/core/classes/String.html#M000771
theIV
ahh, so how do I make it an array? should it be type text?
Elliot
What is the 1430 for? Do you actually want to store an array in your database or just the text 'up'?
theIV
the 1430 is actually two seperate IDs, so the user can vote up, on those items, in their array of votes.so unless its a huge mistake which I dont understand - I was planning on an array in the db?
Elliot
A: 

I'm splitting off the comments as I don't want to hijack Evil Trouts response and we seem to be veering away from what was initially spoken about.

Unfortunately I get this error: NoMethodError

You have a nil object when you didn't expect it! The error occurred while evaluating nil.votes

For this, have a look at the authlogic example application. My guess is that you don't have the appropriate before_filter set up and aren't requiring the user to be logged in on that action.

the 1430 is actually two seperate IDs

I don't fully follow this. It's a concatenation of two IDs? What are the IDs for? To be honest, I've never used an array as being a field type in my database so I don't know what the advantages of it are, but whenever I think to myself that an array would be a good idea, I usually question whether or not it wouldn't be better to just have it be a separate model, and hence, table.

It sounds like the situation you are describing might have a Question which a User can vote up on. If so, I might have a separate Voteable model which would join the users with the questions they can vote 'up' on.

Maybe if you provide some more insight into this side of things, I can make a better suggestion. Cheers.

theIV