既然ActiveRecord::Relation#all在Rails4中已被弃用,那么如何迭代所有记录呢?
过去是这样的:
Foo.all.each do |foo|
# whatever
end
我现在可以像这样近似,但它感觉很脏:
Foo.where(true).each do |foo|
# whatever
end
有没有更好的方法?
我是第一次尝试rails、neo4j.rb和rspec,所以对于你们这些知识渊博的人来说,这可能是一个非常简单的解决方案。这基本上就是我所做的:
(I am using jruby)
rails new graphtest -m http://andreasronge.github.com/neo4j/rails.rb
cd graphtest
bundle
rails generate model testnode
rails generate rspec:install
rspec spec/models/testnode_spec.rb
然后我在运行rspec:NoMethodError
我们将应用程序从Rails 4迁移到Rails 5,并在Docker中运行。在我们的主机系统上,我们当然有git二进制文件,但在Docker中没有。在Docker中运行Puma会给出这个错误消息,我想摆脱它:
bin/rails: No such file or directory - git
Rspec将显示以下错误消息:
/usr/local/bundle/bin/rspec: No such file or directory - git
哪个gem对git有依赖?或者它是核心的Rails gem之一,我应该忽略这个错误消息吗?
嗨,我正在尝试rspec嘲笑下面的课程:
class Person
def initialize(personid)
Rails.logger.debug "Creating person with id #{personid}"
end
end
使用此方法:
require 'spec_helper'
describe Person do
describe "#initialize" do
let(:rails_mock) { double("Rails
我在我的应用程序中使用了rspec-rails 2.11.0,但是当我执行rspec spec/时发生了一些错误。这是一堆错误:
/Users/diego/.rvm/gems/ruby-1.9.3-head@global/gems/bundler-1.1.4/lib/bundler/runtime.rb:31:in `block in setup': You have already activated rspec-core 2.10.1, but your Gemfile requires rspec-core 2.11.1. Using bundle exec may solve
我对Rails和RSpec比较陌生。我花了很多时间试图理解RSpec、WebMock、存根和其他相关的东西的基础知识,我认为这些都会对我有帮助,但我就是无法通过这个测试。我非常肯定这是一个非常简单和常见的测试,但不幸的是,我现在已经到了一个点,我只是随机地尝试所有的选项,并希望一个将通过试验和错误。
我有一个非常简单的控制器,它的index操作调用这样的服务:
def index
@some_var = @some_service.status # defined at rails-root/lib/services/SomeService.rb
render :index
end
当
我试图在memcache中保存大量的ids和permalinks,以便不必对相同的数据多次访问数据库。
我的代码:
if Rails.cache.exist?("ids_and_permalinks_array")
data = Rails.cache.fetch("ids_and_permalinks_array")
else
data = []
Page.approved.select('id, permalink').find_each { |f| data << f }
Rails.cache.write(
我在rails中有一个导入控制器,可以将几个具有多个记录的csv文件导入到我的数据库中。我想在RSpec中测试记录是否真的是使用RSpec保存的:
<Model>.any_instance.should_receive(:save).at_least(:once)
然而,我得到的错误是:
The message 'save' was received by <model instance> but has already been received by <another model instance>
一个人为设计的控制器示例:
rows
我希望确保我的EventNotifier对象捕获并记录Rails记录器的异常,因此我使用rspec模拟编写了以下rspec测试:
describe EventNotifier do
before(:all) do
RSpec::Mocks::setup(self)
end
it 'logs exception on error' do
error = RuntimeError.new('simulated for tests')
EventDispatcher.should_receive(:dispatch).with(
我有一个Rails 4应用程序,下面是我的lib/foobar:
jan@rmbp ~/D/r/v/l/foobar> tree
.
├── foo_bar.rb
└── foobar_spec.rb
0 directories, 2 files
和档案:
foobar_spec.rb
require "spec_helper"
describe "FooBar" do
subject { FooBar.new }
its(:foo) { should == "foo"}
#stubbed version of test c
我编写了一个映射器,它与从facebook获得的属性相匹配,并将它们设置为我的模型。
class Mapper::FacebookUser
def initialize (user, facebook_user)
@user = user
@facebook_user = facebook_user
end
def name
@user.name = @facebook_user['name'] if @facebook_user['name'] rescue false
end
end
Facebook的回应是这样的
我在db中有很少的记录,并且需要不时地处理它。但是,此操作占用我的服务器上的所有内存。我是用塞德基克做这个手术的。因此,当这个任务使用所有内存时,我的rails应用程序变得非常慢。
通常(不包括逻辑),我的代码看起来像
Model.each do |m|
//do some logic code here
end
如何使垃圾收集器运行在一定数量的记录(前)。10k记录),这样我就不会面对记忆不足的情况。把它一分为二对我有帮助吗?
这是一个有点奇怪的。
在rails中,有时加载页面需要一段时间(大约12秒)。因此,我试图重写它,所以大的部分使用缓存的查询,而不是它们的关联中的find,我将在下面解释:
在我试图加快速度之前,示例代码:
假设每个用户都属于一个帐户,所以在此之前它是这样的
class User < ActiveRecord::Base
belongs_to :account
end
class Account < ActiveRecord::Base
end
意见:
@users = User.all
<% @users.each do | user | %>
<%
我有一个带有after_create回调的Rails模型,它有与外部API交互的代码。当我运行rspec测试时,该代码正在执行,另一个应用程序上的内容正在创建。
我想做一些事情,比如:
after_create :external_api_code, unless: testing?
def testing?
#what goes here to recognize that the object is being created in a test?
end
我知道find_each被设计成比each占用更小的内存。我发现了其他人很久以前写的一些代码。我认为这是错误的。
想想这个密码。
users = User.where(:active => false) # What this line does actually? Nothing?
users.find_each do |user|
# update or do something..
user.update(:do_something => "yes")
end
在这种情况下,它将将所有用户对象存储到users变量。因此,我们已经填充了全部内存空间
我们正在做一个使用RSpec 2的rails 3.2.1项目。我的问题是,我应该测试我的每个活动记录模型的基本持久性吗?在我的C# / NHibernate时代,我经常这样做,以确保有正确的表/映射。
因此,如果我有一个具有姓名、地址和电话字段的客户,我可能会像这样编写一个rspec:
describe Customer do
it "saves & retrieves its fields to and from the db"
c = Customer.new
c.name = "Bob Smith"
c.addre