给定一个带有default_scope的模型来过滤所有过时的条目:
# == 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
Run Code Online (Sandbox Code Playgroud)
现在我想得到任何票.在这种情况下,with_exclusive_scope是要走的路,但这种方法是否受到保护?只有这个有效:
Ticket.send(:with_exclusive_scope) { find(:all) }
Run Code Online (Sandbox Code Playgroud)
有点黑客,不是吗?那么正确的使用方法是什么?特别是在处理协会时,情况变得更糟(假设用户有很多票):
Ticket.send(:with_exclusive_scope) { user.tickets.find(:all) }
Run Code Online (Sandbox Code Playgroud)
这是如此丑陋! - 不能成为轨道!?
我正在运行Rails 3.2应用程序.我查看了Google网站管理员工具,看到了很多随机页面的HTTP 502错误.奇怪的是,所有这些都?iframe=true&width=80%&height=80%作为查询参数被抓取:
例如http://www.mypage.com/anypage?iframe=true&width=80%&height=80%
当然,我不会在内部链接到那些页面,必须是外部的.检查谷歌,在这里证明我 - 我看到很多其他页面都有相同的问题.
看起来像外部服务创建这些链接,但为什么??
我想存根只是一个特定的模式,但不是唯一的一个特定的对象,不是每个实例
例如给定具有属性'name'(字符串)和'cool'(布尔值)的类'Person'.我们有两种型号:
person_bill:
name: bill
cool: false
person_steve:
name: steve
cool: false
Run Code Online (Sandbox Code Playgroud)
现在我想要只是史蒂夫,这工作正常:
p1 = people(:person_steve)
p1.stubs(:cool? => true)
assert p1.cool? #works
Run Code Online (Sandbox Code Playgroud)
但是,如果我再次从DB加载Model,它不会:
p1 = people(:person_steve)
p1.stubs(:cool? => true)
p1 = Person.find_by_name p1.name
assert p1.cool? #fails!!
Run Code Online (Sandbox Code Playgroud)
这有效,但也影响比尔,不应该:
Person.any_instance.stubs(:cool? => true)
assert people(:person_bill).cool? #doesn't fails although it should
Run Code Online (Sandbox Code Playgroud)
那么我怎么能只是史蒂夫存根,但无论如何?是否有条件的any_instance之类的
Person.any_instance { |p| p.name == 'Steve' }.stubs(:cool? => true)
Run Code Online (Sandbox Code Playgroud)
提前致谢!