Rails:为什么 with_exclusive_scope 受到保护?关于如何使用它有什么好的做法吗? [英] Rails: Why is with_exclusive_scope protected? Any good practice on how to use it?

查看:23
本文介绍了Rails:为什么 with_exclusive_scope 受到保护?关于如何使用它有什么好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个带有 default_scope 的模型来过滤所有过时的条目:

Given a model with default_scope to filter all outdated entries:

# == Schema Information
#
#  id          :integer(4)      not null, primary key
#  user_id     :integer(4)      not null, primary key
#  end_date    :datetime        

class Ticket < ActiveRecord::Base
  belongs_to :user
  default_scope :conditions => "tickets.end_date > NOW()"
end

现在我想要任何票.在这种情况下 with_exclusive_scope 是要走的路,但这种方法是否受到保护?仅此有效:

Now I want to get any ticket. In this case with_exclusive_scope is the way to go, but is this method protected? Only this works:

 Ticket.send(:with_exclusive_scope) { find(:all) }

有点像黑客,不是吗?那么正确的使用方法是什么呢?尤其是在处理关联时,情况变得更糟(假设用户有很多票):

Kind of a hack, isn't? So what's the right way to use? Especially when dealing with associations, it's getting even worse (given a user has many tickets):

 Ticket.send(:with_exclusive_scope) { user.tickets.find(:all) }

太丑了!!!- 不可能是铁轨!?

That's so ugly!!! - can't be the rails-way!?

推荐答案

FYI 对于任何正在寻找 Rails3 方法的人,您可以使用 unscoped 方法:

FYI for anyone looking for the Rails3 way of doing this, you can use the unscoped method:

Ticket.unscoped.all

这篇关于Rails:为什么 with_exclusive_scope 受到保护?关于如何使用它有什么好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆