开发者

Rails moving queries from view to controller

Got this code for view and controller:

# matches/show.html.haml
%h3
  Players

-@clans.each do |clan|
  %h4=link_to clan.name, clan
  %ul
    开发者_运维问答-@players.find_all_by_clan_id(clan.id).each do |player|
      %li
        %strong=link_to player.name, player


%h3
-@rounds.each_with_index do |round,index|
  %h4
    Round
    =index+1

  -@clans.each 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?


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:

-@match.clans.each do |clan|

Otherwise, use a scope if its something more complicated than a simple has_many/belongs_to association.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜