English 中文(简体)
清理控制器以加快应用程序
原标题:Cleaning up controllers to speed up application

因此,在我的应用程序中,我有通知和不同的记录计数,它们在整体布局中使用,因此在每个页面上都需要。

目前,在我的application_controller中,我有很多这样的东西:

@status_al = Status.find_by_name("Alive")
@status_de = Status.find_by_name("Dead")
@status_sus = Status.find_by_name("Suspended")
@status_hid = Status.find_by_name("Hidden")
@status_arc = Status.find_by_name("Archived")
@balloon_active = Post.where(:user_id => current_user.id, :status_id => @status_al.id )
@balloon_dependent = Post.where(:user_id => current_user.id, :status_id => @status_de.id )
@balloon_upcoming = Post.where(:user_id => current_user.id, :status_id => @status_sus.id )
@balloon_deferred = Post.where(:user_id => current_user.id, :status_id => @status_hid.id )
@balloon_complete = Post.where(:user_id => current_user.id, :status_id => @status_arc.id )

.. Thats really just a small piece, I have at least double this with similar calls. The issue is I need these numbers pretty much on every page, but I feel like I m htting the DB wayyyy too many times here.

有什么更好实施的想法吗?

最佳回答

Scopes

首先,您应该将其中许多移动到scope中,这将允许您以更灵活的方式使用它们,例如使用ActiveRecord链接查询。请参阅http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html

Indexes

其次,如果您仍在执行所有这些查询,请确保对数据库进行索引,例如,按名称快速查找Status。完成第一个索引的示例迁移:

add_index :status (or the name of your Status controller), :name

Session

如果您在这里需要的数据不是关键的,即您不需要依赖它来进行进一步的计算或数据库更新,您可以考虑将其中的一些数据存储在用户的会话中。如果你这样做了,你可以简单地在未来从会话中读取你需要的任何内容,而不是在每次页面加载时都访问你的数据库。

如果此数据是关键数据和/或必须更新到第二个数据,请避免此选项。

Counter Caching

如果您需要定期进行某些记录计数,请考虑设置计数器_cache。基本上,在您的模型中,您可以执行以下操作:

Parent.rb
has_many :children

Child.rb
belongs_to :parent, :counter_cache => true

确保parent表有一个名为child_count的字段,Rails会在每个子表的创建/删除时为您更新该字段。如果您使用counter_cache,您将避免访问数据库以获取计数。

注意:使用counter_cache会导致稍微更长的创建和销毁操作,但如果您经常使用这些计数,通常值得使用counter_case。

问题回答

对此,您应该只需要一个数据库查询,类似于:

@posts = Post.where(:user_id => current_user.id).includes(:status)

然后使用Enumerable#group_by将帖子收集到不同的类别中:

posts_by_status = @posts.group_by do {|post| post.status.name }

这会给你一个散列:

{ Alive  => [...],  Dead  => [...]}





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签