I’m about to lose my mind due to a simple Rails where query. I simply cannot understand why it does work like 10 lines ago and does not after it. I could not figure out what might be causing the problem
@userID = Token.where(:tokenCode => @tokenReceived)
#@init.tokenCode=@tokenReceived+"1" #randomize algorithm required!
@init.tokenCode=@codeGenerated=generate_activation_code()
if @userID.nil?
@newToken=Token.new
@newToken.tokenCode=@codeGenerated
else
@tokenToAdd = "12"
@newToken=Token.where(:userID => "1")
#if @newToken.nil?
@newToken.tokenCode="12"
#end
end
#@newToken.save
@init.save
When I make a successful JSON request to ‘http://localhost:3000/inits.json’ it gives me a page with tons of erros but I think the main error among those are:
<h1>
NoMethodError
in InitsController#create
</h1>
<pre>undefined method `tokenCode=' for #<ActiveRecord::Relation:0x007fc43cb40b88></pre>
What could be the reason? Am I writing the where clause all wrong?
Edit: When I activate the if clause it works. I simply believe the @newToken object is null, however it is almost impossible for me to detect why. There is a data in my Token table with userID 1.
When you do:
You get an
ActiveRecord::Relation, but you expect an object. So simply replace it with: