Rails Active Record ID 与对象 ID +活动::关系
我一直收到这样的消息:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ticket = Ticket.where(:number => Ticket_id)
返回 ActiveRecord::Relation(在 IRB 中求值时,执行数据库查询并返回票证数组)。因此,ticket.id
尝试对整个票证数组(而不是一张实际票证)执行.id
。也许您只想要第一个结果?
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). Soticket.id
is trying to perform.id
on the entire array of tickets, not one actual ticket.Maybe you only want the first result?