在Rails 4中使用has_many_through语句不推荐使用警告

时间:2013-06-23 19:30:12

标签: ruby-on-rails-4

我有以下关系

class User
  has_many :relationships
  has_many :friends, :through => :relationships, -> select: 'friends.*,    relationships.weight', order: 'weight DESC'

当我升级到Rails 4时,我收到以下警告:

DEPRECATION WARNING: The following options in your Service.has_many :friends declaration are deprecated: :order,:select.

我该如何解决这个问题?一般来说,Rails 4中是否有正在进行的参考?

1 个答案:

答案 0 :(得分:2)

在Rails 4中,您在普通User.where(...)样式查询中看到的任何选项现在都将以lambda格式显示。其中包括:order:select

has_many :friends, -> { select("friends.*, relationships.weight").order("weight desc") }, :through => :relationships

请注意,Proc确实需要是has_many的第二个参数,因此:through =>部分需要保持在最后。

相关问题