我的Event
型号
def event_tokens=(ids)
events = ids.split(',')
allowed_events = []
events.each do |i|
i = i.strip
event = Event.where("upper(name) = ?", i.upcase);
if event.present?
allowed_events << event.id #line w/ error
else
print "this doesnt exist, add in staging table " + i.to_s
end
end
self.event_ids = allowed_events
end
上面代码中的event.id
正在发出警告
警告:对象#id将是 弃用;使用Object#object_id
当我将event.id
更改为event[:id]
时,我收到了错误
符号作为数组索引
答案 0 :(得分:0)
event = Event.where("upper(name) = ?", i.upcase);
...返回一个集合(从技术上讲,它是一个集合的代理),而不是一个ActiveRecord对象。
尝试:
events = Event.where("upper(name) = ?", i.upcase)
event = (events.blank? ? nil : events.first)