小编Don*_*ato的帖子

使用Mongoid和Ruby查询过去30天的日期范围?

如何使用Mongoid和Ruby查询日期范围(比如说从现在开始的最近30天)?

我需要最终得到如下所示的数组或哈希:

{
    15 => 300,
    14 => 23,
    13 => 23
    ...
    30 => 20  # Goes over into previous month
    28 => 2
}
Run Code Online (Sandbox Code Playgroud)

我目前正在使用DateTime实例以及unix时间戳整数字段存储每个文档.

上述哈希中的键是天数,值是这些天的所有销售额的总和.

有任何想法吗?

ruby date range mongodb mongoid

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

使用devtools调试器返回脚本中的上一行

我正在使用谷歌浏览器提供的devtools调试器.在执行控制方面,它允许您在脚本中的任何位置添加断点,甚至允许您将断点附加到事件.您可以在脚本中逐行执行.您可以跳过功能以保持相同的功能或步入功能.当你进入函数时,你可以退出函数返回调用者.

我遇到的一个问题是,当我跳过一个函数调用时,我想回到它,以便进入该函数调用.我读过https://developer.chrome.com/devtools/docs/javascript-debugging,我找不到办法做到这一点.我是否可以忽略任何东西,或者有没有办法"退后一步"?

javascript

12
推荐指数
2
解决办法
4934
查看次数

PHPStorm,Xdebug和谷歌Chrome调试

我花了一整天的时间在这上面仍然无法让调试器在使用谷歌浏览器的PHPStorm中工作.以下是我采取的步骤:

1)安装后找到xdebug扩展:

locate xdebug.so
# => /usr/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
Run Code Online (Sandbox Code Playgroud)

2)在php.ini文件中插入扩展名:

php –ini
# => Configuration File (php.ini) Path: /etc
# => Loaded Configuration File:         /etc/php.ini
sudo vim /etc/php.ini
zend_extension=/usr/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so
Run Code Online (Sandbox Code Playgroud)

3)确认xdebug正在工作:

php -v
PHP 5.4.30 (cli) (built: Jul 29 2014 23:43:29) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
Run Code Online (Sandbox Code Playgroud)

4)转到PHPStorm>首选项>语言和框架> PHP.单击与"Interpreter:"相同的行上的展开图标(方框).输入php可执行文件路径.单击刷新框以弹出调试器版本.单击确定并应用.

在此输入图像描述

5)切换PHPStorm中的"开始侦听PHP调试连接"按钮.

在此输入图像描述

6)在源代码中设置断点.

在此输入图像描述

7)转到此网址https://www.jetbrains.com/phpstorm/marklets/.选择Xdebug下的Generate.将"启动调试器","停止调试器"和"调试此页面"拖到Google Chrome中的书签栏中.

在此输入图像描述

8)然后我在浏览器中重新加载网页,完全没有中断.我甚至尝试使用xdebug谷歌浏览器扩展程序,但这也不起作用.然而,在他们的视频中,它有效:

https://www.jetbrains.com/phpstorm/documentation/phpstorm-video-tutorials.jsp#10

我在这里错过了什么?

根据下面的答案,这是phpinfo对xdebug扩展的含义:

xdebug

xdebug support  enabled
Version 2.2.3
IDE …
Run Code Online (Sandbox Code Playgroud)

php phpstorm

10
推荐指数
1
解决办法
8405
查看次数

activerecord不喜欢查询

我找不到相当于"不喜欢"的活跃记录.我能够找到一个where.not,但是会检查字符串是否与值不匹配,如下所示:

User.where.not(name: 'Gabe')

is the same as:

User.where('name != ?', 'Gabe')
Run Code Online (Sandbox Code Playgroud)

我正在寻找一个NOT LIKE,其中的值不包含在字符串中.等效的sql查询如下所示:

SELECT * FROM users WHERE name NOT LIKE '%Gabe%'
Run Code Online (Sandbox Code Playgroud)

在ActiveRecord中,我现在可以使用以下内容:

User.where("name NOT LIKE ?", "%Gabe%")
Run Code Online (Sandbox Code Playgroud)

但这还有很多不足之处.Rails 4的任何新增功能都可以实现这一目的吗?

activerecord ruby-on-rails

10
推荐指数
2
解决办法
5516
查看次数

aapt以非零退出值127结束

2天,我一直试图获得一个空白的android活动,以便在64位Ubuntu 14.04上的intellij idea终结时进行编译.这个错误让我很难过:

Error:Gradle: Execution failed for task ':app:mergeDebugResources'.
> /home/guarddog/Documents/github/IcsTrac/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.2.1/res/drawable-ldrtl-hdpi/abc_ic_ab_back_mtrl_am_alpha.png: Error: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/local/android-sdk-linux/build-tools/22.0.1/aapt'' finished with non-zero exit value 127
Run Code Online (Sandbox Code Playgroud)

注意我有Java 8,Gradle 2.5和/usr/local/android-sdk-linux/tools我的所有$PATH.

我在这里尝试了这个建议:在Android Studio中Gradle构建失败了.但错误仍然存​​在.我该如何解决这个问题?

android

6
推荐指数
1
解决办法
6262
查看次数

UTF-8是Ruby v.2中的默认编码吗?

Matz在他的书中写道,为了使用UTF-8,你必须在脚本的第一行添加编码注释.他举了一个例子:

# -*- coding: utf-8 -*-   # Specify Unicode UTF-8 characters

# This is a string literal containing a multibyte multiplication character
s = "2x2=4"

# The string contains 6 bytes which encode 5 characters
s.length        # => 5: Characters:  '2'   'x'   '2'   '='   '4'
s.bytesize      # => 6: Bytes (hex): 32   c3 97  32    3d    34 
Run Code Online (Sandbox Code Playgroud)

当他调用时bytesize,它返回,6因为乘法符号×在ascii集之外,并且必须由具有两个字节的unicode表示.

我尝试了练习而没有指定编码注释,它将乘法符号识别为两个字节:

'×'.encoding
 => #<Encoding:UTF-8> 
'×'.bytes.to_a.map {|dec| dec.to_s(16) }
 => ["c3", "97"] 
Run Code Online (Sandbox Code Playgroud)

所以看起来utf-8是默认编码.这是Ruby 2的最新成员吗?他的例子来自Ruby 1.9.

ruby encoding

5
推荐指数
1
解决办法
1783
查看次数

将自定义查询作为活动记录关系返回

我写了这个自定义查询,我认为使用活动记录查询接口不能很好地转换它:

SELECT * FROM dockets 
INNER JOIN entries 
ON entries.docket_id = dockets.id 
WHERE NOT EXISTS 
( 
  SELECT 1 FROM entries AS e 
  WHERE e.docket_id = dockets.id 
  AND e.docket_type REGEXP 'Key1|Key2|Key3|Key4' 
)
Run Code Online (Sandbox Code Playgroud)

我可以使用connection.execute以下命令执行此查询:

sql = "SELECT * FROM dockets INNER JOIN entries ON entries.docket_id = dockets.id WHERE NOT EXISTS ( SELECT 1 FROM entries AS e WHERE e.docket_id = dockets.id AND e.docket_type REGEXP 'Key1|Key2|Key3|Key4' )"
Docket.connection.execute(sql)
Run Code Online (Sandbox Code Playgroud)

问题是不返回 ActiveRecord::Relation。它给了我一个 MySQL::Result 对象。因此我不能向它附加关系方法:

Docket.connection.execute(sql).limit(10)
NoMethodError: undefined method `limit' for #<Mysql2::Result:0x007fcd1dde6330> 
Run Code Online (Sandbox Code Playgroud)

是否有另一个接口可以用来返回 ActiveRecord::Relation …

activerecord ruby-on-rails

5
推荐指数
1
解决办法
1152
查看次数

Rails 从多态的belongs_to对象构建关联

我有一个多态关系如下:

class Profile
  belongs_to :practice, polymorphic: :true
end

class ForeclosurePractice
  has_one :profile, as: :practice
end  
Run Code Online (Sandbox Code Playgroud)

我想根据我拥有的配置文件构建一个练习对象,但不幸的是实践返回零:

p = Profile.new
p.practice # => nil
Run Code Online (Sandbox Code Playgroud)

如何从 Profile 对象构建练习对象?

ruby-on-rails

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

引导标签左对齐

我有以下标记:

<form accept-charset="UTF-8" action="/task_categories/1" class="form-horizontal" id="edit_task_category_1" method="post"> 
  <div class="row">
    <div class="col-md-4">
      <div class="well">        
        <div class="form-group"><label class="control-label col-sm-6" for="task_category_task_category_users_attributes_0_Enable">Enable?</label><div class="col-sm-6"><input name="task_category[task_category_users_attributes][0][enable]" type="hidden" value="0"><input class="form-control" id="task_category_task_category_users_attributes_0_enable" name="task_category[task_category_users_attributes][0][enable]" type="checkbox" value="true"></div></div>
        <div class="form-group"><label class="control-label col-sm-6" for="task_category_task_category_users_attributes_0_Enable Comment">Enable comment?</label><div class="col-sm-6"><input name="task_category[task_category_users_attributes][0][comments_enabled]" type="hidden" value="0"><input class="form-control" id="task_category_task_category_users_attributes_0_comments_enabled" name="task_category[task_category_users_attributes][0][comments_enabled]" type="checkbox" value="true"></div></div>
        <input id="task_category_task_category_users_attributes_0_user_id" name="task_category[task_category_users_attributes][0][user_id]" type="hidden" value="1">
      </div>
    </div>

    <div class="col-md-4">
      <div class="well">
        <div class="form-group"><label class="control-label col-sm-6" for="task_category_task_category_users_attributes_1_Enable">Enable?</label><div class="col-sm-6"><input name="task_category[task_category_users_attributes][1][enable]" type="hidden" value="0"><input class="form-control" id="task_category_task_category_users_attributes_1_enable" name="task_category[task_category_users_attributes][1][enable]" type="checkbox" value="true"></div></div>
        <div class="form-group"><label class="control-label col-sm-6" for="task_category_task_category_users_attributes_1_Enable Comment">Enable comment?</label><div class="col-sm-6"><input name="task_category[task_category_users_attributes][1][comments_enabled]" type="hidden" value="0"><input class="form-control" …
Run Code Online (Sandbox Code Playgroud)

css twitter-bootstrap

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

ruby正则表达式匹配多次出现的模式

我希望构建一个ruby正则表达式来匹配模式的多次出现并将它们返回到数组中.模式很简单:[[.+]].也就是说,两个左括号,一个或多个字符,后跟两个右括号.

这就是我所做的:

str = "Some random text[[lead:first_name]] and more stuff [[client:last_name]]"
str.match(/\[\[(.+)\]\]/).captures
Run Code Online (Sandbox Code Playgroud)

上面的正则表达式不起作用,因为它返回:

["lead:first_name]] and another [[client:last_name"]
Run Code Online (Sandbox Code Playgroud)

当我想要的是这个:

["lead:first_name", "client:last_name"] 
Run Code Online (Sandbox Code Playgroud)

我想如果我使用非捕获组,肯定它应该解决问题:

str.match(/(?:\[\[(.+)\]\])+/).captures
Run Code Online (Sandbox Code Playgroud)

但非捕获组返回相同的错误输出.关于如何解决我的问题的任何想法?

ruby regex

3
推荐指数
2
解决办法
5108
查看次数