2011-02-01 112 views
3

我一直在收到這樣的消息:Rails活動記錄ID與對象ID +活動::關係

警告:對象#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 

我希望,當我詢問門票這將是一流的ActiveRecord ::基地。我不知道該怎麼做才能實現這一目標,或者我應該向哪個方向發展。

目標:查詢票證,打印其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 2011-02-01 04:18:21