我把记录作为:
def self.imp_broadcast_preview!
Broadcast.where(for_gamers: true).order(:created_at).last
end
然后在我的控制器中,我有:
def index
@conversations = Conversation.where(gamer: @gamer)
@conversations << Broadcast.imp_broadcast_preview!
end
上面的代码Rails 4.2
在会话中正常工作并合并了最后的广播消息。我刚刚将代码库更新为Rails 5.2
,现在出现错误:
NoMethodError (undefined method `<<' for #<Conversation::ActiveRecord_Relation:0x00007fd2541baca0>)
我尝试使用merge
代替,但是由于broadcast
不是activerecord relation
该功能已在Rails 5.0中删除,您可以检查https://github.com/rails/rails/issues/25906。在那里,您将找到为什么删除了它,以及指向删除了该功能的提交的链接。
为了使您的代码正常工作,您应该做的就是将第一个结果转换为Array,这样<<
可以正常工作:
def index
@conversations = Conversation.where(gamer: @gamer).to_a
@conversations << Broadcast.imp_broadcast_preview!
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句