我正在优化我们的Rails应用程序中的一些慢速事务,并且我看到了渲染JSON视图所花费的大量时间:
Rendered welcome/index.json.rabl (490.5ms)
Completed 200 OK in 1174ms (Views: 479.6ms | ActiveRecord: 27.8ms)
Run Code Online (Sandbox Code Playgroud)
假设API调用正在返回它需要返回的数据,那么在rails中呈现JSON的最快方法是什么?
我们使用Rabl是因为它能够轻松共享代码,但我们并不依赖它.
我有一个SQL Server 2000数据库,大约有220个表.这些表之间有许多外键关系.通过性能分析,我们发现许多这些外键关系都缺少索引.我不想对性能问题做出反应,而是积极主动地找到所有缺少索引的外键.
如何以编程方式确定哪个外键缺少索引?
我after_action在邮件中使用回调来记录发送的电子邮件.电子邮件通过延迟作业发送.这是有效的,除非我们无法访问远程服务器 - 在这种情况下,电子邮件不会被发送,但我们记录它是.延迟作业稍后重试该电子邮件,并且已成功发送,但我们已记录了已发送的两封电子邮件.
它看起来像这样:
class UserMailer < ActionMailer::Base
after_action :record_email
def record_email
Rails.logger.info("XYZZY: Recording Email")
@user.emails.create!
end
def spam!(user)
@user = user
Rails.logger.info("XYZZY: Sending spam!")
m = mail(to: user.email, subject: 'SPAM!')
Rails.logger.info("XYZZY: mail method finished")
m
end
end
Run Code Online (Sandbox Code Playgroud)
我这样调用这个代码(使用延迟的可执行邮件程序):
UserMailer.delay.spam!( User.find(1))
Run Code Online (Sandbox Code Playgroud)
当我在调试器中逐步执行此操作时,似乎在传递邮件之前调用了我的after_action方法.
[Job:104580969] XYZZY: Sending spam!
[Job:104580969] XYZZY: mail method finished
[Job:104580969] XYZZY: Recording Email
Job UserMailer.app_registration_welcome (id=104580969) FAILED (3 prior attempts) with Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 1025
Run Code Online (Sandbox Code Playgroud)
如何在我的邮件程序方法中捕获网络错误并记录电子邮件尝试失败,或者什么都不做?我正在使用Rails 4.2.4.
我想使用Module#related(https://github.com/37signals/concerning - Rails 4.1的一部分)来定义类方法.这样我就可以将单个类使用的模块移回到类中.
但是,似乎我无法定义类方法.鉴于这种:
class User < ActiveRecord::Base
attr_accessible :name
concerning :Programmers do
module ClassMethods
def programmer?
true
end
end
end
module Managers
extend ActiveSupport::Concern
module ClassMethods
def manager?
true
end
end
end
include Managers
end
Run Code Online (Sandbox Code Playgroud)
我希望这两个工作:
User.manager?
User.programmer?
Run Code Online (Sandbox Code Playgroud)
但第二次提出
NoMethodError: undefined method `programmer?' for #<Class:0x007f9641beafd0>
Run Code Online (Sandbox Code Playgroud)
如何使用Module#关于?定义类方法?
我正在使用cancan来授权我的控制器操作.其中一个由cancan授权访问的类是一个树,使用acts_as_ancestry实现.我在使用load_and_authorize_resource不允许用户访问根级别时遇到问题,而是允许从内部节点开始访问.
以下是一些相关类定义:
class User < ActiveRecord::Base
belongs_to :organization, :inverse_of => :users
end
class Post < ActiveRecord::Base
belongs_to :organization, :inverse_of => :posts
end
class Organization < ActiveRecord::Base
has_ancestry :cache_depth => true
has_many :users, :inverse_of => :organization
has_many :posts, :inverse_of => :organization
end
Run Code Online (Sandbox Code Playgroud)
管理帖子的规则是"您可以管理您下面任何组织中的帖子".我的康康技能定义是这样的:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
# subtree_ids is added by acts_as_ancestry
can :manage, Post, {:organization_id => user.organization.subtree_ids}
end
end
Run Code Online (Sandbox Code Playgroud)
在控制器中,我有这个(省略其他操作)
class PostsController < ApplicationController
load_and_authorize_resource :post
def index …Run Code Online (Sandbox Code Playgroud) 我们的postgres数据库有两个模式:公共模式和元数据模式.我在测试数据库中需要两个模式,但rake db:schema:dump只转储公共模式.如果我添加 schema_search_path: "public, metadata"到我的database.yml文件,它会转储两个模式,但架构信息不存在.
我如何转储两个模式,db/schema.rb以便我可以加载它们rake db:test:prepare?
我开始从Rails 4.1.4升级到Rails 4.2.0.它看起来像第一个!某些活动记录关联不再支持.
发生了什么事first!(在ActiveRecord的::协会:: CollectionProxy),使现在失败?
如何修复行为,使其像4.1.4一样工作?
Rails 4.1:
(byebug) user.organization.registration_codes
#<ActiveRecord::Associations::CollectionProxy [#<RegistrationCode id: 259, code: "AWESOMESAUCE" ... >]>
(byebug) user.organization.registration_codes.first!
#<RegistrationCode id: 259, code: "AWESOMESAUCE" ... >
Run Code Online (Sandbox Code Playgroud)
Rails 4.2:
(byebug) user.organization.registration_codes
#<ActiveRecord::Associations::CollectionProxy [#<RegistrationCode id: 259, code: "AWESOMESAUCE" ... >]>
(byebug) user.organization.registration_codes.first!
NoMethodError Exception: undefined method `[]' for nil:NilClass
nil
Run Code Online (Sandbox Code Playgroud)
更新
深入研究ActiveRecord,我发现它失败了:
def find_nth(index, offset)
if loaded?
@records[index]
else
offset += index
@offsets[offset] ||= find_nth_with_limit(offset, 1).first
end
end
Run Code Online (Sandbox Code Playgroud)
loaded?返回true,但@records为零.抛出调试器并调用find_nth_with_limit(offset, 1).first返回我期望的记录.
first!是在finder_methods.rb定义在活动记录的问题似乎是,该协会认为其加载,但@records为零
ruby ruby-on-rails ruby-on-rails-4 rails-activerecord ruby-on-rails-4.2
我正在用selenium测试一个django应用程序,我的一个页面使用了jquery ui tabs元素.其中一个选项卡包含一个列出一些用户的简单表,并通过ajax加载.使用该应用程序时,该选项卡工作得很好,但是当使用selenium自动执行测试时,该选项卡似乎不会加载它的内容!我自己在python中编写测试.起初,我正在使用selenium RC 的click方法,但正如我从先前的测试中学到的那样,当涉及到锚标签时,这是相当错误的,所以我采用了之前使用的解决方案:wait_for_condition方法和明确地调用了tab单击事件(甚至是load事件!)然而选项卡仍然无效!
我在这里绝望,我的大多数测试依赖于那个页面,其中几乎一半都在那张桌子上,但是,唉,似乎selenium搞砸了javascript!(我在课堂上有其他测试,它们运行得很好,所以在服务器级别没有任何奇怪的事情,这似乎是由客户端的selenium引起的问题)我的测试代码与此类似:
class TestMyApp(TransactionTestCase):
urls = 'myapp.test_urls'
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "http://localhost:8000/")
self.selenium.start()
#self.selenium.set_speed(2000)
self.selenium.window_maximize()
def test_users_list(self):
"""Test that an app's users are correctly listed"""
sel = self.selenium
users = []
for u in range(settings.FREE_USER_LIMIT/2):
users.append(self.app.users.create(name="testUser_%s"%uuid4()))
sel.open("/")
sel.wait_for_page_to_load("30000")
sel.wait_for_condition('selenium.browserbot.getCurrentWindow().jQuery("#tabs").tabs("select",1);\
selenium.browserbot.getCurrentWindow().jQuery("#tabs").tabs("load",1);',
3000)
for user in users:
try: self.failUnless(sel.is_text_present(user.name))
except AssertionError, e: self.verificationErrors.append(str(e))
try: self.failUnless(sel.is_text_present(str(user.added.date())))
except AssertionError, e: self.verificationErrors.append(str(e))
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
Run Code Online (Sandbox Code Playgroud) actionmailer ×1
cancan ×1
delayed-job ×1
django ×1
foreign-keys ×1
jquery-ui ×1
json ×1
postgresql ×1
python ×1
ruby ×1
selenium-rc ×1
sql ×1
sql-server ×1
unit-testing ×1