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 9145643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:33:57+00:00 2026-06-17T10:33:57+00:00

Possible Duplicate: What is the order of ActiveRecord callbacks and validations? I comes from

  • 0

Possible Duplicate:
What is the order of ActiveRecord callbacks and validations?

I comes from the background of Java. One thing I think very weird in Rails is that you can set up call back functions like attributes just under the class, such as before_filter.

class SomeController < ActionController::Base
  before_filter Proc.new {.....}
end

I don’t really understand how it works. I found this post which explains before_filter . I understand the flow of logic and it’s just a method. But I still don’t understand when will before_filter being executed to set up the callback chain.

  • 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-06-17T10:33:58+00:00Added an answer on June 17, 2026 at 10:33 am

    before_filter is not a Ruby feature, it is a class method provided by Ruby on Rails (the web framework) that you can use in your controllers to execute a piece of code before executing any action in your controller.

    So, how does Ruby on Rails does that?

    When you are defining a class in Ruby you are actually executing code, try this in irb:

    class Hello
      puts "defining hello..."
    
      def greet
        puts "Hello there"
      end
    end
    

    You’ll see that “defining hello…” gets printed in the terminal when you define the class. You have not instantiated any object, you just have defined a class, but you can execute any code in the middle of defining a class.

    You know that you can define “class methods” and “instance methods”, and what’s interesting is that you can call your class methods while you are still defining your class:

    class MyClass
      def self.add_component(component)
        # Here @@components is a class variable
        @@components ||= []        # set the variable to an empty array if not already set.
        @@components << component  # add the component to the array
      end
    
      add_component(:mouse)
      add_component(:keyboard)
      add_component(:screen)
    
      def components
        @@components # The @@ gets you the class variable
      end
    end
    
    MyClass.new.components
    => [:mouse, :keyboard, :screen]
    

    def self.add_component defines a class method that you can call while still defining your class. In this example add_component adds a keyboard to a list in a class variable, and the def components instance method (which is called on an instance of this class) access this class variable. The class method could have been defined in a parent class, and it would have worked the same. That example may be a little bit weird.

    Let’s do another example.

    class RubyOnSlugsController
      def self.before_filter(callback)
        @@callbacks ||= []
        @@callbacks << callback
      end
    
      def execute_callbacks
        @@callbacks.each { |callback| callback.call() }
        return "callbacks executed"
      end
    end
    
    class MyController < RubyOnSlugsController
      before_filter Proc.new { puts "I'm a before filter!" }
      before_filter Proc.new { puts "2 + 2 is #{2 + 2}" }
    end
    
    controller = MyController.new
    controller.execute_callbacks
    

    will output:

    I'm a before filter!
    2 + 2 is 4
    => "callbacks executed"
    

    Ruby on Rails does something similar (but quite more complex) with before_filter, and it makes sure that all callbacks you define with it are called before your normal controller methods.

    I hope this clears things a little bit for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java: strange order of queue made from priority queue I tired to
Possible Duplicate: Can one do a for each loop in java in reverse order?
Possible Duplicate: Why is this strange order happens in PriorityQueue in java? Please, see
Possible Duplicate: Java static class initialization in what order are static blocks and static
Possible Duplicate: Java: System.out.println and System.err.println out of order Why this code System.err.println(err); System.out.println(out);
Possible Duplicate: Java: Checking equality of arrays (order doesnt matter) I have two arrays
Possible Duplicate: How do you retrieve items from a dictionary in the order that
Possible Duplicate: Sort arrays of primitive types in descending order Java : How to
Possible Duplicate: Given a 2d array sorted in increasing order from left to right
Possible Duplicate: Order of execution of parameters guarantees in Java? If I have a

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.