Rails 将查询从视图移动到控制器
得到了视图和控制器的代码:
# matches/show.html.haml
%h3
Players
[email protected] do |clan|
%h4=link_to clan.name, clan
%ul
[email protected]_all_by_clan_id(clan.id).each do |player|
%li
%strong=link_to player.name, player
%h3
[email protected]_with_index do |round,index|
%h4
Round
=index+1
[email protected] do |clan|
%h4=clan.name
%ul
-round.participations.includes(:player,:champion).find_all_by_clan_id(clan.id).each do |participation|
%li
=participation.player.name
=participation.champion.name
# matches_controller.rb
class MatchesController < ApplicationController
def index
@matches = Match.played.includes(:clans).page(params[:page])
end
def show
@match = Match.includes(:rounds,:clans).find(params[:id])
@clans = @match.clans
@rounds = @match.rounds
@players = @match.players
end
end
如何将所有不必要的数据库查询、逻辑等从视图移动到控制器?
也许以某种方式简化这个?
Got this code for view and controller:
# matches/show.html.haml
%h3
Players
[email protected] do |clan|
%h4=link_to clan.name, clan
%ul
[email protected]_all_by_clan_id(clan.id).each do |player|
%li
%strong=link_to player.name, player
%h3
[email protected]_with_index do |round,index|
%h4
Round
=index+1
[email protected] do |clan|
%h4=clan.name
%ul
-round.participations.includes(:player,:champion).find_all_by_clan_id(clan.id).each do |participation|
%li
=participation.player.name
=participation.champion.name
# matches_controller.rb
class MatchesController < ApplicationController
def index
@matches = Match.played.includes(:clans).page(params[:page])
end
def show
@match = Match.includes(:rounds,:clans).find(params[:id])
@clans = @match.clans
@rounds = @match.rounds
@players = @match.players
end
end
How do I move all unnecessary db queries, logic etc. from view to controller?
Maybe simplify this somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其移至 Match 模型,而不是控制器。您的控制器通常应仅包含路由逻辑并设置要在视图中使用的实例变量。大部分代码应该放在模型中。瘦控制器,胖模型。
阅读范围:
另外,这篇文章来自 2006 年(范围前),但其中大部分内容仍然与您的问题相关:
http://weblog.jamisbuck.org/2006/10/18 /skinny-controller-fat-model
你真的不需要为部落、回合和玩家设置实例变量。在迭代关联集合的简单情况下,您可以直接从 @match 访问这些内容。例如:
否则,如果比简单的 has_many/belongs_to 关联更复杂,则使用作用域。
Move it to the Match model, not the controller. Your controller should usually only contain routing logic and setting an instance variable to use in the view. The bulk of the code should go in the model. Skinny controllers, fat models.
Read up on scopes:
http://edgerails.info/articles/what-s-new-in-edge-rails/2010/02/23/the-skinny-on-scopes-formerly-named-scope/index.html
Also, this article is from 2006 (pre scopes), but most of it is still relevant to your question:
http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model
You really don't need to set instance variables for clans, rounds and players. You can access these directly from @match in simple situations where you are iterating through an associated collection. For example:
Otherwise, use a scope if its something more complicated than a simple has_many/belongs_to association.