2012-09-11 4 views
0

나는 단순한 레일로 인해 내 마음을 잃을 것입니다 어디 쿼리. 10 선 전과 같이 작동하는 이유를 이해할 수 없으며 그 후에는 이해하지 못합니다. 나는에 성공적으로 JSON 요청을 할 때 문제레일스 : Where 절이 어떤 조건에서도 작동하지 않습니다

@userID = Token.where(:tokenCode => @tokenReceived) 
    #@[email protected]+"1" #randomize algorithm required! 
    @[email protected]=generate_activation_code() 
    if @userID.nil? 
    @newToken=Token.new 
    @[email protected] 
    else 
    @tokenToAdd = "12" 
    @newToken=Token.where(:userID => "1") 
    #if @newToken.nil? 
     @newToken.tokenCode="12" 
    #end 
    end 
    #@newToken.save 
    @init.save 

의 원인이 될 수있는 것을 알아낼 수 '에 http : // localhost를 : 3000/inits.json'이하는 오류하지만 톤으로 나에게 페이지를 제공합니다 그 중 주요 오류가 있다고 생각하십시오 :

<h1> 
    NoMethodError 
    in InitsController#create 
</h1> 
<pre>undefined method `tokenCode=&#x27; for #&lt;ActiveRecord::Relation:0x007fc43cb40b88&gt;</pre> 

어떤 이유가있을 수 있습니까? where 절을 모두 잘못 쓰고 있습니까?

편집 : if 절을 활성화하면 작동합니다. 나는 단순히 @newToken 객체가 null이라고 믿는다. 그러나 왜 내가 그 이유를 발견하는 것은 거의 불가능하다. 이 사용자 ID 1.

답변

2

당신이 내 토큰 테이블의 데이터입니다 :

@newToken=Token.where(:userID => "1") 

당신은 ActiveRecord::Relation를 얻을 수는 있지만 객체를 기대합니다. 간단히 다음과 같이 바꾸십시오 :

@newToken=Token.where(:userID => "1").first 
+0

엄청난 답변을 해주셔서 감사합니다! 니가 내 목숨을 구했어. – Ali