Rails Active Record ID or Object ID + Active :: Relation

I get messages like this:

warning: Object # id will be deprecated; use Object # object_id

I read and tried to do tricks from Ruby Object # id and Active Record without success:

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

I would expect that when I request a ticket, this will be the ActiveRecord :: Base class. I am not sure what to do to achieve this, or if I have a direction that I must enter.

Purpose: request for a ticket, printing its identifier. In the above example, the id value should be 97.

+3
source share
1 answer

ticket = Ticket.where(:number => ticket_id) ActiveRecord:: Relation ( IRB ). ticket.id .id , .

, ?

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

Source: https://habr.com/ru/post/1789152/


All Articles