Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6336797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:07:36+00:00 2026-05-24T19:07:36+00:00

I have this terribly large controller in my app. I’d really like to make

  • 0

I have this terribly large controller in my app. I’d really like to make it as skinny as possible. Below is some of the code, showing the types of things I’m currently doing.. I’m wondering what things I can move out of this?

A note – this is not my exact code, a lot of it is similar. Essentially every instance variable is used in the views – which is why I dont understand how to put the logic in the models? Can models return the values for instance variables?

  def mine

    #For Pusher
    @push_ch = "#{current_user.company.id}"+"#{current_user.id}"+"#{current_user.profile.id}"

    #Creating a limit for how many items to show on the page
    @limit = 10
    if params[:limit].to_i >= 10
      @limit = @limit + params[:limit].to_i     
    end

    #Setting page location
    @ploc="mine"

    @yourTeam = User.where(:company_id => current_user.company.id)
    #Set the user from the param
    if params[:user]
      @selectedUser = @yourTeam.find_by_id(params[:user])
    end

    #Get all of the user tags
    @tags = Tag.where(:user_id => current_user.id)    

    #Load the user's views
    @views = View.where(:user_id => current_user.id)

    if !params[:inbox]

         #Hitting the DB just once for all the posts
         @main_posts = Post.where(:company_id => current_user.company.id).includes(:status).includes(:views)
         @main_posts.group_by(&:status).each do |status, posts|
           if status.id == @status.id
             if @posts_count == nil
               @posts_count = posts
             else
               @posts_count = @posts_count + posts
             end
           elsif status.id == @status_act.id
             if @posts_count == nil
               @posts_count = posts
             else
               @posts_count = @posts_count + posts
             end
           end
         end

         if params[:status] == "All" || params[:status] == nil
           @posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).order(sort_column + " " + sort_direction).where(:company_id => current_user.company.id, :status_id => [@status.id, @status_act.id, @status_def.id, @status_dep.id, @status_up.id]).limit(@limit).includes(:views)
         else
           @posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).order(sort_column + " " + sort_direction).where(:company_id => current_user.company.id).limit(@limit).includes(:views)
         end

    elsif params[:inbox] == "sent"

         @yourcompanylist = User.where(:company_id => current_user.company.id).select(:id).map(&:id)
          @yourcompany = []
          @yourcompanylist.each do |user|
            if user != current_user.id
            @yourcompany=@yourcompany.concat([user])  
            end
          end

          if params[:t]=="all"
            @posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
          elsif params[:status]!="complete"
            @posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
          elsif params[:status]!=nil
            @posts = Post.search(params[:search]).status_filter(params[:status]).user_filter(params[:user]).tag_filter(params[:tag], current_user).order(sort_column + " " + sort_direction).where(:user_id => current_user.id).includes(:views, :tags).limit(@limit)
          end

    end

    respond_to do |format|
      format.html # index.html.erb
      format.js # index.html.erb
      format.xml  { render :xml => @posts }
    end
  end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:07:36+00:00Added an answer on May 24, 2026 at 7:07 pm

    You can start by moving logic into the model…

    A line like this screams of feature envy:

    @push_ch = "#{current_user.company.id}"+"#{current_user.id}"+"#{current_user.profile.id}"
    

    I would recommend moving it into the model:

    #user.rb
    def to_pusher_identity
      "#{self.company_id}#{self.id}#{self.profile_id}"
    end
    

    And then in your controller

    @push_ch = current_user.to_pusher_identity
    

    At this point you could even move this into a before_filter.

    before_filter :supports_pusher, :only => :mine
    

    Another thing you can do is create richer associations, so you can express:

    @tags = Tag.where(:user_id => current_user.id)    
    

    as

    @tags = current_user.tags
    

    Another example would be for main posts, instead of

    Post.where(:company_id => current_user.company.id).includes(:status).includes(:views)
    

    you would go through the associations:

    current_user.company.posts.includes(:status).includes(:views)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in jQuery, that I want to reimplement with the prototype
I currently have this code: /// <summary> /// This is an ineffecient method of
I have this quite long query that should give me some information about shipments,
I have some large csv files (1.5gb each) where I need to replace specific
I have this piece of code that searches for a folder given a starting
I have data that looks like this: > head(data) date price volume 1 2011-06-26
I have been working to deploy a relatively large Rails app (Rails 2.3.5) and
I have some code that builds SQL queries to perform a lenient, catch-all kind
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.