Rails Active Record ID 与对象 ID +活动::关系

发布于 2024-10-15 16:38:22 字数 1382 浏览 1 评论 0原文

我一直收到这样的消息:

warning: Object#id will be deprecated;使用 Object#object_id

我阅读并尝试了 Ruby Object#id 警告和 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::Base 类。我不知道该怎么做才能实现这一目标,也不知道这是否是我应该前进的方向。

目标:查询票证,打印其 ID。在上面的示例中,id 的值应为 97。

I've been receiving messages like this:

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

I read and attempted the tricks from Ruby Object#id warnings 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'd expect that when I queried for ticket it would be of class ActiveRecord::Base. I'm not sure what to do to get that going or if its the direction I should head in.

Goal: Query for a ticket, print its id. In the example above, the id's value should be 97.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请持续率性 2024-10-22 16:38:22

ticket = Ticket.where(:number => Ticket_id) 返回 ActiveRecord::Relation(在 IRB 中求值时,执行数据库查询并返回票证数组)。因此,ticket.id 尝试对整个票证数组(而不是一张实际票证)执行 .id

也许您只想要第一个结果?

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

ticket = Ticket.where(:number => ticket_id) returns an ActiveRecord::Relation (which when evaluated in IRB, performs the database query and returns an array of tickets). So ticket.id is trying to perform .id on the entire array of tickets, not one actual ticket.

Maybe you only want the first result?

>> ticket = Ticket.where(:number => ticket_id).first
>> puts ticket.id
=> 97
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文