加入范围模型



我没有找到有关该文件的文档。我有两种型号,TokenConnection

class Connection < ActiveRecord::Base
  belongs_to :token
  scope :failed, -> { where('...') }
end
class Token < ActiveRecord::Base
  has_many :connections
end

我知道我们可以像这样的Tokens.joins(:connections)加入,但我正在尝试做类似Token.joins(:connections => :failed)的事情您认为可能吗?

我刚找到

Token.joins(:connexions).merge(Connection.failed)

您可以在连接模型上添加默认范围,例如

class Connection < ActiveRecord::Base
    belongs_to :token
    default_scope where(' Your condition ')
end

因此,Tokens.joins(:connections)只会为您提供与您的条件范围符合您的条件的令牌。您还可以检查此链接以及https://apidock.com/rails/activerecord/base/default_scope/class

最新更新