小编loc*_*red的帖子

如何使用ActiveRecord将一个表中的两列指向另一个表中的同一列?

我冒着手掌到额头的风险,但我无法弄清楚如何使用Rails的ActiveRecord糖来做到这一点.

我有一个tickets表有两列(submitter_idassignee_id),每列应该从users表中引用不同的用户(特别是表中的idusers).我希望能够做类似的事情ticket.submitter.nameticket.assignee.email使用ActiveRecord的关联.提交者和受让人只是不同关联名称下的用户对象.

我发现的唯一与我正在做的事情接近的是使用多态关联,但最后我很确定它并不是我真正需要的.我不会有多种类型,提交者和受让人都是用户,很可能是两个不同的用户.

任何帮助都会很棒.谢谢!

ruby activerecord ruby-on-rails associations multiple-tables

8
推荐指数
2
解决办法
2561
查看次数

如何在Ruby中有效地解析大型文本文件

我正在编写一个导入脚本来处理可能有数十万行(日志文件)的文件.使用一种非常简单的方法(下面)花了足够的时间和记忆,我觉得它会在任何时候取出我的MBP,所以我杀了这个过程.

#...
File.open(file, 'r') do |f|
  f.each_line do |line|
    # do stuff here to line
  end
end
Run Code Online (Sandbox Code Playgroud)

这个文件特别有642,868行:

$ wc -l nginx.log                                                                                                                                        /code/src/myimport
  642868 ../nginx.log
Run Code Online (Sandbox Code Playgroud)

有没有人知道处理这个文件中每一行的更有效(内存/ CPU)方式?

UPDATE

上面的代码f.each_line简单地将正则表达式与行匹配.如果匹配失败,我将该行添加到@skipped数组中.如果它通过,我将匹配格式化为哈希(由匹配的"字段"键入)并将其附加到@results数组.

# regex built in `def initialize` (not on each line iteration)
@regex = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - (.{0})- \[([^\]]+?)\] "(GET|POST|PUT|DELETE) ([^\s]+?) (HTTP\/1\.1)" (\d+) (\d+) "-" "(.*)"/

#... loop lines
match = line.match(@regex)
if match.nil?
  @skipped << line
else
  @results << convert_to_hash(match)
end
Run Code Online (Sandbox Code Playgroud)

我对这是一个效率低下的过程完全开放.我可以convert_to_hash使用预先计算的lambda 代码,而不是每次都计算出计算结果.我想我只是假设行迭代本身就是问题,而不是每行代码.

ruby text-processing

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

Rails中的问题保存模型

我正在建立一个带评论的简单博客.有一个Post模型和一个Comment模型.除了创建新注释之外,两者之间的每次交互都很好.在尝试发布新评论时,我在Rails中遇到问题:

wrong number of arguments (1 for 0)
Run Code Online (Sandbox Code Playgroud)

以下是请求参数(来自堆栈跟踪):

{"commit"=>"Post My Comment",
 "post_id"=>"1",
 "authenticity_token"=>"5a7b3ad3dda771d4ef5eb5850018649a8be2af11",
 "comment"=>{"name"=>"bj",
 "website"=>"asdf.com",
 "comment"=>"asdf",
 "email"=>"asdf@asdf.com"}}
Run Code Online (Sandbox Code Playgroud)

并且完整的堆栈跟踪:

/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:296:in `notify'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:296:in `callback'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/callbacks.rb:263:in `valid?'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/validations.rb:900:in `save_without_dirty'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/dirty.rb:75:in `save_without_transactions'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:79:in `transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:98:in `transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:118:in `rollback_active_record_state!'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/transactions.rb:106:in `save'
app/controllers/comments_controller.rb:39:in `create'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1162:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1162:in `perform_action_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:580:in `call_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:573:in `perform_action_without_benchmark'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/rescue.rb:201:in `perform_action_without_caching'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/query_cache.rb:8:in `cache'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:529:in `send'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:529:in `process_without_filters'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:569:in `process_without_session_management_support'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/session_management.rb:130:in `process'
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:389:in `process'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:76:in `process'
/Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/rails.rb:74:in …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord model ruby-on-rails

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

创建ActiveRecord模型时出现问题:保存中缺少数据

我正在开发的Sinatra应用程序中使用ActiveRecord在数据库中创建新的模型行时遇到问题.正在创建的对象没有任何错误(使用save!,没有引发异常),但是我为保存指定的大多数数据都不存在.

class ProjectMeta < ActiveRecord::Base
    attr_accessor :completion_ratio, :num_stories, :num_completed_stories, :original_target_date, :current_target_date

    ...

    def self.create_from_project(project)
        meta = ProjectMeta.new
        meta.project_id = project.id
        meta.num_stories = project.num_stories
        meta.num_completed_stories = project.num_completed_stories
        meta.completion_ratio = ProjectMeta.calculate_ratio(project.num_completed_stories, project.num_stories)
        meta.current_target_date = project.current_target_date
        meta.save!
        meta
    end

    ...

end
Run Code Online (Sandbox Code Playgroud)

对我正在发送的项目对象的数据进行的所有检查以及我正在创建的新元对象都显示数据存在.但是当我meta.inspect在保存之前和之后执行操作时,它会显示所有数据(除了project_id)都处于默认状态(零).我也检查过meta.errors.nil?,保存后没有任何错误.

最令人费解的是,如果我转向并使用该project_id获取新的元实例并将数据放入其中,则可以为数据库节省任何问题.

这让我感到很沮丧,因为我已经使用ActiveRecord在Rails和Sinatra中构建了几个站点.这个问题让我感到困惑.谁能告诉我我做错了什么?

ruby activerecord model sinatra

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

printf()似乎正在破坏我的数据

我正在用C编写一个nginx模块,并且有一些非常奇怪的结果.我从我的模块中提取了一个函数来测试它的输出以及相关的nginx类型/宏定义.

我正在我的build_key_hash_pair函数中构建一个结构,然后printf()对其中的内容进行处理main.当我printf在内部函数里面的数据时,main输出是有效的.当我删除printf内部函数内部时,main打印一个空字符串.这很令人困惑,因为在调用build_key_hash_pairI 之后,除了显示它之外,我没有对数据进行操作.这是代码:

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

typedef struct ngx_str_t {
    size_t          len;
    char            *data;
} ngx_str_t;

typedef uintptr_t   ngx_uint_t;

typedef struct key_hash_pair {
    ngx_uint_t      hash;
    ngx_str_t       key;
} key_hash_pair;

#define ngx_string(str)     { sizeof(str) - 1, (char *) str }
#define ngx_str_set(str, text)                                               \
    (str)->len = sizeof(text) - 1; (str)->data = (char *) text
#define ngx_hash(key, c)    ((ngx_uint_t) key * 31 + …
Run Code Online (Sandbox Code Playgroud)

c gdb nginx

3
推荐指数
1
解决办法
1045
查看次数

"Dialyzer is usually never wrong", but I can't figure out how my @spec is incorrect

I have some code that is failing dialyzer and I cannot understand why. No matter what I put into the @spec at the top of the function, calls to that function return a puzzling dialyzer error. Here is a simplification of the function. As far as I can tell, I have spec'd the function correctly.

@spec balances(uuid :: String.t(), retries :: non_neg_integer) ::
        {:ok, list()}
        | {:internal_server_error, String.t(), String.t()}
        | {:internal_server_error, map | list, String.t()}
def balances(uuid, retries \\ 0) …
Run Code Online (Sandbox Code Playgroud)

elixir dialyzer

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