小编Nic*_*ett的帖子

关闭生产中的活动记录日志记录

如何关闭生产中的详细活动记录?(就像记录每个SQL语句一样)我的日志填满的速度非常快,因为我有后台作业运行大量查询.这在我的开发服务器上很好,但我不需要这个登录生产.

logging ruby-on-rails ruby-on-rails-4 rails-activerecord

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

将ActiveRecord :: Relation转换为模型

非常初学的问题.我正在使用Rails 3的查询界面,如下所示:

class User < ActiveRecord::Base

def self.authenticate
 if Rails.env = 'development'
   self.where('username = ?', 'development_user')
 else
   self.where('username = ?', request.env['REMOTE_USER'])
 end
end

end
Run Code Online (Sandbox Code Playgroud)

这是返回一个ActiveRecord :: Relation对象,实际上我想要与查询相关的User对象.如何将其转换为User对象?

ruby-on-rails

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

ActiveRecord在脏(未保存)关系上使用.where()

假设我有这种关系

车型/ person.rb

class Person
  belongs_to :group
end
Run Code Online (Sandbox Code Playgroud)

车型/ group.rb

class Group
  has_many :people
end
Run Code Online (Sandbox Code Playgroud)

现在我创建一个人并分配一个新组

group = Group.create(name: 'Group A')
group.person.new(name: 'John')
group.person.new(name: 'Lucy')
# Now if I call group.person.where(name: 'Lucy') 
# here it will return an empty result, 
# because it has not been persisted to the database.
Run Code Online (Sandbox Code Playgroud)

无论如何在搜索中包含未保存的记录?

我想要这样做的原因是我需要为一个组创建很多人,并且需要在中间执行where()查询以查看我是否已经添加了具有该名称的人.我希望在实例化所有人之后调用group.save,因为它在一个事务中执行,而不是每个人的单个事务,这个事情要慢得多.

activerecord ruby-on-rails ruby-on-rails-4

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

如何同时运行多个nokogiri屏幕刮线程

我有一个网站,需要在许多不同的网站上使用Nokogiri来提取数据.此过程使用delayed_job gem作为后台作业运行.但是,每页运行大约需要3-4秒,因为它必须暂停并等待其他网站响应.我目前只是通过基本上说来运行它们

Websites.all.each do |website|
  # screen scrape
end
Run Code Online (Sandbox Code Playgroud)

我想分批执行它们而不是每个都执行它们,这样我就不必等待来自每个站点的服务器响应(在一段时间内最多可能需要20秒).

什么是最好的红宝石或铁路方式来做到这一点?

感谢您的帮助.

screen-scraping ruby-on-rails nokogiri

5
推荐指数
2
解决办法
1699
查看次数

Symfony2表单生成器 - 从数据库查询创建一系列选项

在我的FormType类中,我在buildForm方法中有这个:

//...
->add('businessUnit', 'entity', array(
                'class' => 'TrainingBundle:Employee',
                'attr' => array('class' => 'form-control select2'),
                'property' => 'businessUnit',
                'empty_value' => 'All Business Units',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('e')
                        ->groupBy('e.businessUnit')
                        ->orderBy('e.businessUnit', 'ASC')
                        ;
                },
                'required' => false
//...
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作,除了将"businessUnit"放入<option>标签的值中,我获得了员工ID.我需要的是一个包含Employee类中所有不同businessUnit的下拉列表.也许我应该使用choice而不是entity,但后来我不知道如何生成选择数组.

答案 如接受的答案所述,我做了这个功能

 private function fillBusinessUnit() {
        $er = $this->em->getRepository('TrainingBundle:Employee');

        $results = $er->createQueryBuilder('e')
               ->groupBy('e.businessUnit')
               ->orderBy('e.businessUnit', 'ASC')
               ->getQuery()
               ->getResult()
               ;

        $businessUnit = array();
        foreach($results as $bu){
             $businessUnit[$bu->getBusinessUnit()] = $bu->getBusinessUnit();
        }

        return $businessUnit;
    }
Run Code Online (Sandbox Code Playgroud)

必须将EntityManager传递给表单.并且还放在 use Doctrine\ORM\EntityManager;表格的顶部

php symfony

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

告诉Rails忽略特定的URL并让Apache处理

我通过Passenger在我的Apache服务器上运行了一个rails应用程序.

偶尔我会为网站使用一些PHP脚本,并将它们放在公共目录中.

当我去/ php /我希望Apache使用PHP解析器处理请求并让rails忽略它.

目前我可以去/php/index.php,它工作正常.但是我需要/ php /才能正常工作但是rails一直在寻找控制器来处理它.

我有一种感觉这与apache重写规则有关,但我无法弄明白.

我用过

RewriteEngine On 
RewriteRule ^php - [L] 
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
Run Code Online (Sandbox Code Playgroud)

在/php/.htaccess文件中,但这不起作用.我仍然得到Rails的页面未找到错误.

apache ruby-on-rails

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