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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:40:11+00:00 2026-05-11T18:40:11+00:00

In models and controllers, we often use Rails macros like before_validation , skip_before_filter on

  • 0

In models and controllers, we often use Rails macros like before_validation, skip_before_filter on top of the class definition.

How is this implemented? How do I add custom ones?

Thanks!

  • 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-11T18:40:11+00:00Added an answer on May 11, 2026 at 6:40 pm

    They’re just standard Ruby functions. Ruby’s flexible approach to syntax makes it look better than it is. You can create your own simply by writing your method as a normal Ruby function and doing one of the following:

    1. putting it somewhere that’s accessible by your controllers such as application.rb

    2. putting it in a file and requiring it in.

    3. mixing the code into a class via the Ruby include keyword.

    That last option is great for model classes and the first option is really only for controllers.

    An Example

    An example of the first approach is shown below. In this example we add code into the ApplicationController class (in application.rb) and use it in the other controllers.

    class BusinessEntitiesController < ApplicationController
    
        nested_within :Glossary
    
        private
    
            #  Standard controller code here ....
    

    The nested_within provides helper functions and variables to help identify the id of the "parent" resource. In effect it parses the URL on the fly and is accessible by every one of our controllers. For example when a request comes into the controller, it is automatically parsed and the class attribute @parent_resource is set to the result of a Rails find. A side effect is that a "Not Found" response is sent back if the parent resource doesn’t exist. That saves us from typing boiler plate code in every nested resource.

    That all sounds pretty clever but it is just a standard Ruby function at heart …

    
        def self.nested_within(resource)
            #
            #   Add a filter to the about-to-be-created method find_parent_id
            #
            before_filter :find_parent_id
        
            #
            #   Work out what the names of things
            #
            resource_name = "#{resource.to_s.tableize.singularize}"
            resource_id = "#{resource_name}_id"
            resource_path = "#{resource.to_s.tableize}_path"
        
            #
            #   Get a reference to the find method in the model layer
            #
            finder = instance_eval("#{resource}.method :find_#{resource_name}")
        
        
            #
            #   Create a new method which gets executed by the before_filter above
            #
            define_method(:find_parent_id) do
                @parent_resource = finder.call(params[resource_id])
        
                head :status => :not_found, :location => resource_path 
                        unless @parent_resource
            end
        end
    

    The nested_within function is defined in ApplicationController (controllers/application.rb) and therefore gets pulled in automatically.

    Note that nested_within gets executed inside the body of the controller class. This adds the method find_parent_id to the controller.

    Summary

    A combination of Ruby’s flexible syntax and Rail’s convention-over-configuration makes this all look more powerful (or weirder) than it actually is.

    Next time you find a cool method, just stick a breakpoint in front of it and trace through it. Ahh Open Source!

    Let me know if I can help further or if you want some pointers on how that nested_within code works.

    Chris

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

Sidebar

Ask A Question

Stats

  • Questions 115k
  • Answers 115k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the Colspan and rowspan attributes to set… May 11, 2026 at 10:21 pm
  • Editorial Team
    Editorial Team added an answer I worked out the missing pieces which seem to work:… May 11, 2026 at 10:21 pm
  • Editorial Team
    Editorial Team added an answer It seems you intend to use something resembling the "extended"… May 11, 2026 at 10:21 pm

Related Questions

For the past time I have been working on a Zend Framework based webshop
Recently thanks to rails' popularity, many people start using activerecord as model. however, before
I've been refactoring my models and controllers in an effort to remove code duplication,
I am stuck in a weird Design problem, I am working on a two
I have worked a bit with Django and I quite like its project/applications model

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.