尝试设置default_scope但是获得"支持在没有块的情况下调用#default_scope将被删除"错误

时间:2016-07-25 01:32:55

标签: ruby-on-rails-4 associations parent default-scope

我正在使用Rails 4.2.3。我有这两个模型

class Address < ActiveRecord::Base
    …
    has_one :user

class User < ActiveRecord::Base
  belongs_to :address
  default_scope includes(:address) 

我想在加载“MyObject”时自动加载我的父关联,但我在“default_scope”行上收到此错误...

Support for calling #default_scope without a block is removed. For example instead of `default_scope where(color: 'red')`, please use `default_scope { where(color: 'red') }`. (Alternatively you can just redefine self.default_scope.)

如何调整“default_scope”行以自动加载我的父关联?

1 个答案:

答案 0 :(得分:0)

  • 您的线路中缺少阻止,请按以下方式更改:

    default_scope { includes(:address) }