我的一位开发人员更新了Nokogiri,当拉动更新的Gemfile时,我bundle install失败了.
? my-project git:(master) bundle install
Fetching source index from https://rubygems.org/
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.3
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.3
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.0.0
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
/Users/me/.rvm/rubies/ruby-2.1.2/bin/ruby extconf.rb --use-system-libraries
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using system libraries.
libxml2 version 2.6.21 or …Run Code Online (Sandbox Code Playgroud) 我使用jquery-select2-4搜索外部数据库,并向用户显示他可以选择的搜索结果.
我有一个在这个jsfiddle上运行的工作版本.
但是,例如,如果只返回1个搜索结果,我想跳过整个选择过程,只需将返回的搜索结果添加到所选选项列表中.根据select2文档,我可以添加一个像这样的新选项:
option = new Option("Sample text", "123", true, true);
select2_element.append(option);
select2_element.trigger('change');
Run Code Online (Sandbox Code Playgroud)
这似乎在某种程度上起作用.但是有一些问题.
id和a text.undefined.我意识到这个问题包含3个方面,但所有3个方面可能都会回到这个问题:
如何以编程方式添加新的jquery-select2-4选项并重置搜索字段?
供您参考,这是我要问的代码的上下文:
var formatRepo, formatRepoSelection, selectRepos;
formatRepoSelection = function(element) {
return element.name + ' ' + element.forks + ' ' + element.id;
};
formatRepo = function(element) {
var markup;
if (!element.loading) {
return markup = element.name + ' ' + element.id;
}
};
selectRepos = function() {
var option, select2_element; …Run Code Online (Sandbox Code Playgroud) 我生成的CSV文件一旦生成就需要在Excel中打开和查看.Excel似乎需要与UTF-8不同的编码.
这是我的配置和生成代码:
csv_config = {col_sep: ";",
row_sep: "\n",
encoding: Encoding::UTF_8
}
csv_string = CSV.generate(csv_config) do |csv|
csv << ["Text a", "Text b", "Text æ", "Text ø", "Text å"]
end
Run Code Online (Sandbox Code Playgroud)
在Excel中打开时,特殊字符未正确显示:
Text a Text b Text æ Text ø Text å
Run Code Online (Sandbox Code Playgroud)
知道如何确保正确编码吗?
我有一个rails应用程序,可以从多个IMAP帐户中获取大量电子邮件.
2个问题:
我的代码:
class FetchMailsJobs
include Sidekiq::Worker
include Sidetiq::Schedulable
tiq { hourly.minute_of_hour(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55) }
def perform(last_occurrence, current_occurrence)
users = User.all
users.each do |user|
if user.imap_accounts.exists?
ImapJob.perform_async(user._id.to_s)
end
end
end
end
class ImapJob
include Sidekiq::Worker
def perform(user_id)
s = Redis::Semaphore.new("fetch_imap_mails_for_#{user_id}".to_sym, connection: "localhost")
if s.lock
user = User.where(_id: user_id).first
emails = ImapMails.receive_mails(user)
s.unlock
end …Run Code Online (Sandbox Code Playgroud) 我的申请中有以下内容:
# some_view_file.html.haml
= best_in_place element, :inbox, :type => :checkbox
# imap_accounts_controller.rb
def update
element = ImapAccount.find(params[:id])
element.update_attributes(params[:imap_account])
respond_with element
end
Run Code Online (Sandbox Code Playgroud)
但布尔模型属性不会更新.也没有抛出错误.知道我的应用程序为什么不将"true"或"false"字符串转换为布尔值.
我已经按照文档中的说明完成了它.但是看起来Rails 4通常默认情况下不会打开来接收来自视图/控制器的布尔值,就像在这篇SO帖子中举例说明的那样.
服务器日志说:
Started PUT "/en/imap_accounts/525188ea83c336a4eb000002" for 127.0.0.1 at 2014-01-12 16:43:22 +0100
Processing by ImapAccountsController#update as JSON
Parameters: {"imap_account"=>{"enable_ssl"=>"false"}, "authenticity_token"=>"mX+Dpghb8nB49qhFTLbGSB2w3pJQg56PBgg8jR7G3/Y=", "locale"=>"da", "id"=>"525188ea83c336a4eb000002"}
MOPED: 127.0.0.1:27017 QUERY database=myapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('518f599683c336fb87000003')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.8680ms
MOPED: 127.0.0.1:27017 QUERY database=myapp_development collection=imap_accounts selector={"_id"=>BSON::ObjectId('525188ea83c336a4eb000002')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.4400ms
MOPED: …Run Code Online (Sandbox Code Playgroud) 我有一个多域Rails 4应用程序,其中request.domainhttp请求确定我公开给定访问者的功能.
我的应用中的每个域都应该由自己的MongoDB数据库提供服务.例如domain1.com由db_for_domain_1等提供.
我可以在MongoDB文档中阅读有关运行时持久性的内容
Mongoid.override_database("db_for_#{request.domain}")
Run Code Online (Sandbox Code Playgroud)
使我能够动态切换数据库.
但是当我绕过Mongoid并使用mongo Shell方法db.collection.insert()时,如何保持持久性呢?我仍然会在我的应用程序中执行此操作.
答案可能在关于集合访问的MongoDB文档中,但我不明白.那么如何在此操作之前/期间切换数据库?:
MyModel.collection.insert({field_1: "Value 1", field_2: "Value 2"})
Run Code Online (Sandbox Code Playgroud) 我有一个包含数百万Order文档的数据库。我用以下方法批量插入它们:
Order.collection.insert([
{:_id=>BSON::ObjectId('5471944843687229cdfb0000'), :status=>"open", :name=> "Benny"},
{:_id=>BSON::ObjectId('5471944843687229cdfc0000'), :status=>"open", :name=> "Allan"}
])
Run Code Online (Sandbox Code Playgroud)
我经常需要更新status订单的属性。使用该方法单独更新它们的效率很低update_attribute。
如何批量更新多个 MongoDB 文档?
所需的解决方案可以用下面的“虚构”代码最好地描述:
# IMPORTANT: The exemplified upsert method does not exist
Order.collection.upsert([
{:_id=>BSON::ObjectId('5471944843687229cdfb0000'), :status=>"closed"},
{:_id=>BSON::ObjectId('5471944843687229cdfc0000'), :status=>"some_other_status"}
])
Run Code Online (Sandbox Code Playgroud)
仅供参考,这篇文章中可能有类似的问题/答案,但老实说我不遵循答案。
我刚刚读了这篇SO帖子:
根据一个答案,MongoDB ID不是随机的,并且可以轻松预测。您甚至可以将其转换为时间戳:
http://www.mongodb.org/display/DOCS/Object+ID
我想知道,是否可以使用gem 将_id(例如52a435840000640002695268或52908452636872eda1000000)转换为时间戳mongoid?
我目前正在运行sidekiq 4.1.2。我从来没有设法能够同时运行多个作业。最近看起来我遇到了 Sidekiq 的故障排除 WIKI 中描述的问题,称为Too many connections to MongoDB。显然,mongoid 3没有正确断开工人的连接。但是,我正在使用mongoid 5.1.3.
我的问题出现在一个作业,而其他一些作业正在运行时,尝试使用查询访问数据库:
Timeout::Error: Timed out attempting to dequeue connection after 30 sec.
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:190:in `wait_for_next!'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:176:in `block in dequeue_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:190:in `wait_for_next!'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:176:in `block in dequeue_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:172:in `loop'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:172:in `dequeue_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:62:in `block in dequeue'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:61:in `synchronize'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:61:in `dequeue'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool.rb:51:in `checkout'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool.rb:107:in `with_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/context.rb:63:in `with_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/operation/executable.rb:34:in `execute'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/collection/view/iterable.rb:80:in `send_initial_query'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/collection/view/iterable.rb:41:in `block in each'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/retryable.rb:51:in `call'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/retryable.rb:51:in `read_with_retry'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/collection/view/iterable.rb:39:in `each'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongoid-5.1.3/lib/mongoid/query_cache.rb:207:in `each'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongoid-5.1.3/lib/mongoid/contextual/mongo.rb:121:in `each'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongoid-5.1.3/lib/mongoid/contextual/mongo.rb:295:in …Run Code Online (Sandbox Code Playgroud) 我想创建一个可用作加密密钥的 32 位字符串。该字符串/密钥应源自纯文本字符串,例如:
'I am a string'
Run Code Online (Sandbox Code Playgroud)
我的方法首先是对其进行哈希处理:
hashed_string = Digest::SHA1.hexdigest('I am a string') # => 'bd82fb0e81ee9f15f5929e0564093bc9f8015f1d'
Run Code Online (Sandbox Code Playgroud)
然后仅使用前 32 个字符:
hashed_string[0..31] # => 'bd82fb0e81ee9f15f5929e0564093bc9'
Run Code Online (Sandbox Code Playgroud)
但是,我觉得必须有更好的方法,并且我不确定是否冒着 2 个输入字符串产生相似键的风险。
什么是更好的方法?我看过这篇文章涉及截断,但在那里找不到吸引我的答案。