2011-02-01 4 views
3

나는 다음과 같은 메시지를 받았습니다 :레일스 활성 레코드 ID 대 객체 ID + 활성 :: 관계

경고 : Object # id는 더 이상 사용되지 않습니다. 사용 개체 번호는 내가 읽은

을 것으로, object_id 및 성공하지 Ruby Object#id warnings and Active Record에서 트릭을 시도 :

108-125-94-123:toptickets johnnygoodman$ rails c 
Loading development environment (Rails 3.0.3) 
>> ticket_id = 8899 
=> 8899 
>> ticket = Ticket.where(:number => ticket_id) 
=> [#<Ticket id: 97, name: "Set Up API to Feed Customer Info into Bronto ", number: "8899", category_id: 15, created_at: "2011-01-31 21:24:29", updated_at: "2011-01-31 21:24:29", position: 20>] 
>> ticket.id 
(irb):3: warning: Object#id will be deprecated; use Object#object_id 
=> 2175680980 
>> ticket[:id] 
TypeError: Symbol as array index 
     from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/relation.rb:363:in `[]' 
     from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/relation.rb:363:in `send' 
     from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.3/lib/active_record/relation.rb:363:in `method_missing' 
     from (irb):4 
>> ticket.class 
=> ActiveRecord::Relation 

내가 티켓 조회 할 때이 클래스 액티브 :: 자료 될 것으로 기대. 나는 그 방향을 향하기 위해 무엇을해야할지 잘 모르겠다. 방향을 바꿔야한다.

목표 : 티켓을 쿼리하고 id를 출력한다. 위 예제에서 ID의 값은 97이어야합니다.

답변

7

ticket = Ticket.where(:number => ticket_id)은 ActiveRecord :: Relation (IRB에서 계산할 때 데이터베이스 쿼리를 수행하고 티켓 배열을 반환)을 반환합니다. 따라서 ticket.id은 실제 티켓이 아닌 전체 티켓 배열에서 .id을 수행하려고합니다.

아마도 첫 번째 결과 만 원할 것입니다.

>> ticket = Ticket.where(:number => ticket_id).first 
>> puts ticket.id 
=> 97 
+1

감사합니다. 나는 그것의 초심자를 알고있다, 그러나 당신이 모를 때 ... – johnnygoodman

관련 문제