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

  • Home
  • SEARCH
  • 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 7672337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:15:44+00:00 2026-05-31T16:15:44+00:00

My question is about controller methods (possibly included from an outside class) that work

  • 0

My question is about controller methods (possibly included from an outside class) that work with instance variables. I frequently use a before_filter in controllers to set up certain variables, e.g.:

class DocumentController < ApplicationController
  before_filter :fetch_document

  def action
    @document.do_something
  end

  private

  def fetch_document
    @document = Document.find(params[:id])
  end
end

I’ve been working on a project in which a few controllers will share some functionality, say, document editing. My first thought was to extract the relevant methods, and get them from application_controller.rb or a separate module. But then I noticed I was writing code that looks like this:

  def fetch_document
    @document = Document.find(params[:id])
  end

  def do_something_to_document
    @document.do_something
  end

This sets off warning bells: do_something_to_document is essentially assuming the existence of @document, rather than taking it as an argument. Is this, in your sage opinions, a bad coding practice? Or am I being paranoid?

Assuming it is an issue, I see two general approaches to deal with it:

  1. Check for the instance var and bail unless it’s set:

    def do_something_to_document
      raise "no doc!" unless @document
      [...]
    end
    
  2. Call the action with the instance var as an argument:

    def do_something_to_document(document)
      [...]
    end
    

2 looks better, because it hides the context of the calling object. But do_something_to_doc will only be called by controllers that have already set up @document, and taking @document as a method argument incurs the overhead of object creation. (Right?) 1 seems hackish, but should cover all of the cases.

I’m inclined to go with 1 (assuming I’m right about the performance issue), even though seeing a list of methods referencing mysterious instance vars gives me hives. Thoughts? Let me know if I can be more clear. (And of course, if this is answered somewhere I didn’t see it, just point me in the right direction…)

Thanks,
-Erik

  • 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-31T16:15:45+00:00Added an answer on May 31, 2026 at 4:15 pm

    If you really need document in different controllers, I’d do something like this:

    class ApplicationController < ActionController::Base
      private
      def document
        @document ||= Document.find(params[:document_id])
      end
    end
    
    class FooController < ApplicationController
      before_filter :ensure_document, :only => [:foo]
    
      def foo
        document.do_something
      end
    
      private
      # TODO: not sure if controller_name/action_name still exists
      def ensure_document
        raise "#{controller_name}##{action_name} needs a document" unless document
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question about controllers. Can controller call it`s own class methods inside an action? EDIT:
i have a quetion about MVC routing. I have a controller method that is
Question about subclassing in matlab, under the new class system. I've got class A
One question about protected constructor. I learnt that the protected constructor can be used
I recently asked a question about IIf vs. If and found out that there
I would like to use [Authorize(Roles=Admin)] tags on my controller methods. If a user
This is a follow up to a previous question that I had before about
I have the following situation: From within a view controller, I go about creating
Question about Eclipse RCP and whole perspective/view/editor design - what is the best way
Question about GridView sorting in VB.NET: I have a GridView with AutoGenerateColumns = True

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.