用于自动生成的测试:
test "should create item" do
login_user
assert_difference('Item.count') do
post :create, item: { creator: @item.creator, title: @item.title, user_id: @item.user_id, text: 'Hello, world!' }
end
assert_redirected_to(assigns(:item))
end
Run Code Online (Sandbox Code Playgroud)
Rails 文档没有任何描述.这种方法的目的是什么以及如何使用它?
例:
result = ActiveRecord::Base.connection.execute("select 'ABC'")
Run Code Online (Sandbox Code Playgroud)
我怎样才能从中获得'ABC'价值result?尝试result.first没有成功.谢谢
ps宝石:
activerecord(2.3.9)
mysql(2.8.1)
我正在寻找一种方法,无论是在Ruby还是Javascript中,它都会在字符串中为正则表达式提供所有匹配,可能重叠.
假设我有str = "abcadc",我希望找到a后跟任意数量字符的事件,然后是c.我正在寻找的结果是["abc", "adc", "abcadc"].有关如何实现这一目标的任何想法?
str.scan(/a.*c/)会给我的["abcadc"],str.scan(/(?=(a.*c))/).flatten会给我的["abcadc", "adc"].
有什么比这简单的方法
if hash.key?('a')
hash['a']['b'] = 'c'
else
hash['a'] = {}
hash['a']['b'] = 'c'
end
Run Code Online (Sandbox Code Playgroud) ruby hash variable-assignment autovivification hash-of-hashes
class C1
def pr
puts 'C1'
end
end
class C2 < C1
def pr
puts 'C2'
super
puts self.method(:pr).source_location
end
end
c = C2.new
c.pr
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,是否可以获取由super(C1::pr在我们的例子中)执行的代码的位置以及我们C2::pr使用source_location方法获得代码的位置?
除了作为最后一个参数的哈希之外,您可以在方法调用中删除Ruby中的括号并获得一致的结果(您仍然需要考虑优先级).
但是,我遇到了一个不是这种情况的例子:
''.split(/ ./) # => []
''.split /./ # => []
''.split / ./ # !> SyntaxError: unexpected '.'
Run Code Online (Sandbox Code Playgroud)
这是一个错误/回归(我用2.1.2测试它 - > 2.4.1 Rubys)?
是否有其他一般情况下丢弃parens不能按预期工作?
报道了,让我们看看.
更新:票有点含糊不清.目前尚不清楚它是否是一个bug,但它不会得到修复,%r{}并建议在这些情况下使用.原因确实是开头的斜线被解释为分裂.
我有以下表格:
work_units - 自我解释workers - 自我解释skills - 如果您想要工作,每个工作单位都需要一些技能.每个工人都精通各种技能.work_units_skills - 加入表workers_skills - 加入表工作人员可以请求下一个适当的免费最高优先级(无论这意味着)分配给她的工作单元.
目前我有:
SELECT work_units.*
FROM work_units
-- some joins
WHERE NOT EXISTS (
SELECT skill_id
FROM work_units_skills
WHERE work_unit_id = work_units.id
EXCEPT
SELECT skill_id
FROM workers_skills
WHERE worker_id = 1 -- the worker id that made the request
)
-- AND a bunch of other conditions
-- ORDER BY something complex
LIMIT 1
FOR UPDATE SKIP LOCKED;
Run Code Online (Sandbox Code Playgroud)
这种情况使查询慢了8-10倍.
是否有更好的方式来表达work_units技能应该是技能的一部分workers或改善当前查询的东西?
更多背景: …
sql postgresql performance set-operations relational-division
我正在使用Rails 5和Ruby 2.4.我怎么能弄明白,或者你能看出下面的内容,是否有多个线程在同一时间运行?
pool = Concurrent::FixedThreadPool.new(1)
promises = links.map do |link|
Concurrent::Promise.execute(executor: pool) do
result = process_link(link)
if result
if result.kind_of?(Array)
result.each do |my_obj|
my_obj.update_attributes({ :a => a })
records_processed = records_processed + my_obj.matches.count
end
else
records_processed = records_processed + result.matches.count
result.update_attributes({ :a => a })
end
end
end
end
promises.map(&:wait).map(&:value!)
Run Code Online (Sandbox Code Playgroud)
由于我已将我的池设置为"1",我的假设是没有任何内容同时运行,但我不断收到此错误...
Error during processing: (ActiveRecord::ConnectionTimeoutError) could not obtain a connection from the pool within 5.000 seconds (waited 5.002 seconds); all pooled connections were in use
/Users/nataliab/.rvm/gems/ruby-2.4.0@global/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:202:in `block in wait_poll' …Run Code Online (Sandbox Code Playgroud) 我正在尝试class="text"在输入字段中使用名为:hinted in simple_form 2.0.0.rc的自定义包装器
config.wrappers :hinted do |b|
b.use :input, :class => "text"
end
Run Code Online (Sandbox Code Playgroud)
但是输出没有那个类,我试过了
:wrap_with => {:class => 'text'}
Run Code Online (Sandbox Code Playgroud)
无济于事
有谁知道这是怎么做的?
谢谢!
我有一个进程返回一个结果集,我想使用rspec测试其有效性.该过程将根据参数返回不同的结果,但有许多示例对所有这些都是通用的,因此我想创建一组可以针对所有这些示例运行的常见示例.
我知道首选的做法是使用let来构建结果.问题是每个过程需要一两分钟来生成结果,我可能有30个例子.与所有基于不同的参数排列的我运行约500 examples.If我不得不重建的结果为每比如,试验将运行超过一天.
所以相反,我在before(:all)块中构建一个结果,并将它分配给这样的属性:
RSpec.describe 'Test Description' do
attr_reader :result
before(:all)
@result = build_result({some_parameters})
end
context 'Some context' do
it 'Looks lik a result' do
expect(result.something).to ...
end
it 'Feels lik a result' do
expect(result.something).to ...
end
end
end
Run Code Online (Sandbox Code Playgroud)
也许有比使用属性更好的方法.我想做这样的事情:
RSpec.describe 'Test Description' do
attr_reader :result
before(:all)
@result = build_result({some_parameters})
end
context 'Some context' do
it_behaves_like "A result" result
end
end
Run Code Online (Sandbox Code Playgroud)
在此上下文中使用属性失败.有没有不同的方法来做到这一点?
ruby ×6
activerecord ×1
concurrency ×1
hash ×1
inheritance ×1
javascript ×1
minitest ×1
mysql ×1
performance ×1
postgresql ×1
promise ×1
regex ×1
rspec ×1
simple-form ×1
sql ×1
super ×1