小编Xim*_*mik的帖子

ArgumentError:您需要提供至少一个验证:if

我有一个简单的模型

class Task < ActiveRecord::Base
  validates :deadline, :if => :deadline_in_future?

  def deadline_in_future?
    Date.today < self.deadline
  end
end
Run Code Online (Sandbox Code Playgroud)

一切似乎都好,但是当我在我的rails控制台时

irb(main):001:0> Task.new
ArgumentError: You need to supply at least one validation
Run Code Online (Sandbox Code Playgroud)

问题出在哪儿?

validation ruby-on-rails ruby-on-rails-3.1

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

Facebook如何在没有标签的情况下发送通知

  1. 转到Facebook并允许HTML5桌面通知.
  2. 关闭Facebook标签.
  3. 请某人在facebook上给你写信.

现在你看到facebook上的通知,而Facebook网站没有打开.怎么样?

html5 html5-notifications

13
推荐指数
1
解决办法
3540
查看次数

如果使用SIGSTOP停止其中一个子进程,则子进程在终止时会死亡

我的测试代码是

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main() {
  int c = fork();
  if (c == 0) while(1);
  c = fork();
  if (c == 0) while(1);
  c = fork();
  if (c == 0) while(1);
  c = fork();
  if (c == 0) while(1);
  while(1);
}
Run Code Online (Sandbox Code Playgroud)

所以我有一个父母和4个孩子.当我杀死父母时,孩子们正在使用init作为父母.但如果我(与SIGSTOP)停止其中一个孩子然后杀死父母,孩子们也会被杀死.为什么会这样?

c unix linux

12
推荐指数
1
解决办法
2163
查看次数

Rails:Content_for部分

我的布局中有这样的东西

...
<%= yield :test %>
...
<%= render :partial => 'user/bar' %>
Run Code Online (Sandbox Code Playgroud)

在user/bar.html.erb中我有

<% content_for :test do %>
stuff
<% end %>
Run Code Online (Sandbox Code Playgroud)

而这似乎不起作用.我发现它yield :test在部分之前执行,但在执行动作视图之后执行.为什么会这样做,我该怎么办?

activeview ruby-on-rails-3

9
推荐指数
2
解决办法
6192
查看次数

非缓冲stdin阅读

我的测试应用程序是

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

int main(int argc, char *argv[], char *envp[]) {
  int fd[2];

  if(pipe(fd) < 0) { 
    printf("Can\'t create pipe\n");
    exit(-1); 
  }

  pid_t fpid = fork();
  if (fpid == 0) {
    close(0);
    close(fd[1]);
    char *s = (char *) malloc(sizeof(char));
    while(1) if (read(fd[0], s, 1)) printf("%i\n", *s);
  }
  close(fd[0]);
  char *c = (char *) malloc(sizeof(char));
  while (1) {
    if (read(0, c, 1) > 0) write(fd[1], c, 1); …
Run Code Online (Sandbox Code Playgroud)

c unix linux posix

7
推荐指数
2
解决办法
4374
查看次数

和和乘法模

我有大的数字K,C[1],C[2],C[3]等我算算B:

b = C[1]*C[2]+C[3]*C[4]+... (mod K)
Run Code Online (Sandbox Code Playgroud)

现在我计算全部金额然后做出类似的东西

b = SUM % K.
Run Code Online (Sandbox Code Playgroud)

但是当SUM变得大于无符号长限时,这不起作用,所以我必须使用类似的东西

b = (C[1]*C[2] %K + C[3]*C[4] %K ) %K
Run Code Online (Sandbox Code Playgroud)

但这很费时间.我试过使用unsigned long long,除了unsigned long,这也很耗时.有没有更好的方法?

UPD:

  C = (unsigned long long int *) malloc(N*sizeof(unsigned long long int));
  unsigned long int i, j, l;
  C[0] = 1;
  for (i=1; i<=N; i++) {
    C[i] = 0;
    l = (unsigned long int) i/2;
    for (j=0; j<l; j++) {
      C[i] += C[j]*C[i-j-1];
      C[i] = C[i] …
Run Code Online (Sandbox Code Playgroud)

c algorithm math

6
推荐指数
3
解决办法
1067
查看次数

ActiveRecord:使用组计数

我有一个带has_many评论的帖子模型.每个帖子都有一个线程ID(但没有名为Thread的模型).

所以,如果我想计算这篇文章中的线程数,我就会喜欢

> post.comments.count(:thread, :distinct => true)
SELECT COUNT(DISTINCT "comments"."thread") FROM "comments" WHERE "comments"."post_id" = 3
Run Code Online (Sandbox Code Playgroud)

这很好用.但是,如果我只想用一条评论计算线程数呢?

> post.comments.group(:thread).having('COUNT(*) == 1').count
SELECT COUNT(*) AS count_all, thread AS thread FROM "comments" WHERE "comments"."post_id" = 3 GROUP BY thread HAVING COUNT(*) == 1 ORDER BY id
Run Code Online (Sandbox Code Playgroud)

所以我有一个OrderedHash而不是Integer.我必须做不必要的步骤

> post.comments.group(:thread).having('COUNT(*) == 1').count.count
Run Code Online (Sandbox Code Playgroud)

有没有更好的解决方案?

sql activerecord ruby-on-rails

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

RabbitMQ插件删除重复的消息

我有一个RabbitMQ队列用于生成文档.基本上,每个文件都有typestate(新,处理,准备),所以我用像路由键的话题交换type.state.每次文档更改时,我都会将带有上一个文档描述的消息发送到交换机,并且它的工作状态非常好

但有时文档可以处理两次:

  1. 用户发送新文件.所以新消息report.new被发送到交换.
  2. 虽然worker尚未启动文档处理(队列尚未到达),但用户更新了文档.report.new发送同一文档的新消息.
  3. 因此,现在工作人员得到第一条消息并开始他的工作,而文档被更改,因此这项工作完全没有意义.

现在我只是将小代码添加到工作程序中,将last_modified消息中的文档密钥与数据库中的密钥进行比较,如果它们不相同则查询消息.但是我不认为这是最好的解决方案.

我的想法是添加ID到消息头并有一些RabbitMQ插件,它将ID从队列中删除相同的旧消息.

谢谢.

PS也许另一个MQ引擎在这里有用吗?例如,ActiveMQ可能有这样的功能吗?

message-queue rabbitmq

6
推荐指数
2
解决办法
5484
查看次数

如何在 C 中捕获进程输出?

有没有用 C 语言模拟 PHP 系统的情况?

man system说,system返回命令的状态,但我需要输出(就像在 PHP 中一样)。

当然,我可以使用管道来实现这一点,但是有什么标准的方法吗?

c unix linux posix system

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

UTF-8和HTML实体

我尝试用PHP从Word .DOC文件中弹出文本.一切似乎都没问题,但唯一的麻烦就是这样

&#x0421;&#x0423;&#x0414;&#x041e;&#x0412;&#x0410; &#x0411;&#x0423;&#x0425;&#x0413;&#x0410;&#x041b;&#x0422;&#x0415;&#x0420;&#x0406;&#x042f;
Run Code Online (Sandbox Code Playgroud)

而不是俄罗斯文本.我试过使用html_entity_decode和utf8_encode,但他们没有帮助.有没有简单的解决方案?

php utf-8

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