小编Rng*_*Tng的帖子

Rails:为什么with_exclusive_scope受到保护?关于如何使用它的任何好的做法?

给定一个带有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)

这是如此丑陋! - 不能成为轨道!?

scope ruby-on-rails

47
推荐指数
3
解决办法
3万
查看次数

Arduino:轻量级压缩算法,用于将数据存储在EEPROM中

我想用ATmega168/ATmega328微控制器将大量数据存储到我的Arduino上,但不幸的是,只有256 KB/512 KB的EEPROM存储空间.

我的想法是利用压缩算法去除大小.但是,我对压缩算法的了解非常少,而且我对即用型库的搜索失败了.

那么,是否有一种优化存储大小的好方法?

compression embedded algorithm atmega arduino

17
推荐指数
2
解决办法
2万
查看次数

那些具有"iframe = true&width = 80%&height = 80%"查询参数的请求有什么用?

我正在运行Rails 3.2应用程序.我查看了Google网站管理员工具,看到了很多随机页面的HTTP 502错误.奇怪的是,所有这些都?iframe=true&width=80%&height=80%作为查询参数被抓取:

例如http://www.mypage.com/anypage?iframe=true&width=80%&height=80%

当然,我不会在内部链接到那些页面,必须是外部的.检查谷歌,在这里证明我 - 我看到很多其他页面都有相同的问题.

看起来像外部服务创建这些链接,但为什么??

rack ruby-on-rails http request

8
推荐指数
1
解决办法
1980
查看次数

Rails测试和摩卡:如何存根特定模型 - 条件any_instance?

我想存根只是一个特定的模式,但不是唯一的一个特定的对象,不是每个实例

例如给定具有属性'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)

提前致谢!

ruby unit-testing ruby-on-rails mocha.js

4
推荐指数
1
解决办法
7222
查看次数