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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:46:19+00:00 2026-06-06T21:46:19+00:00

I have this create method: def create … grid = Tag.find_by_id(story[:tag_id]) or raise GridNotFoundError

  • 0

I have this create method:

def create
    ...
    grid = Tag.find_by_id(story[:tag_id]) or raise GridNotFoundError
    ...
    event = Event.find_by_id(story[:event_id]) or raise EventNotFoundError
    ...
  rescue GridNotFoundError
    flash.now[:error] = "Please select a category"
    @item = item
    @years = story[:years]
    @event_name = race[:name]
    @country = race[:country]
    @classification = race[:class]
    @events = Event.all
    @countries = Tag.countries
    @classifications = Classification.all
    @grids = Tag.grids.find(:all, :conditions => ["value != ?", "Channel Creation Grid"])
    render "home/race_updates"
  rescue EventNotFoundError
    flash.now[:error] = "Please select an event or create a new one if you don't find your event"
    @item = item
    @event = story[:event_id]
    @years = story[:years]
    @events = Event.all
    @countries = Tag.countries
    @classifications = Classification.all
    @grids = Tag.grids.find(:all, :conditions => ["value != ?", "Channel Creation Grid"])
    render "home/race_updates"
  rescue CreateEventError
    flash.now[:error] = "There has been a problem creating your event."
    params[:expand] = true
    @item = item
    @years = story[:years]
    @event_name = race[:name]
    @country = race[:country]
    @classification = race[:class]
    @events = Event.all
    @countries = Tag.countries
    @classifications = Classification.all
    @grids = Tag.grids.find(:all, :conditions => ["value != ?", "Channel Creation Grid"])
    render "home/race_updates"
  rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound
    flash.now[:error] = item.errors.full_messages.join(" ,")
    @item = item
    @event = story[:event_id]
    @years = story[:years]
    @events = Event.all
    @countries = Tag.countries
    @classifications = Classification.all
    @grids = Tag.grids.find(:all, :conditions => ["value != ?", "Channel Creation Grid"])
    render "home/race_updates"
  end

and as you can see, the rescues are almost the same. The rescues are also literal copy-pastes of the home#race_updates method.

I’ve got 2 questions:

  1. Is there any way for me to DRY this up?
  2. Is this a good pattern for controllers in general?

I’ve thought of separating it out as a function but I would need to pass in the flash message, item, story and race variables. I feel like it’s not an elegant solution, but it would certainly be cleaner.

I’ve found that coding this way (i.e. raising errors and rescuing them) makes it easier for me to separate the actual business logic as it should be, and handling the different errors/cases that comes up in the business logic. So far, it works, but I want to gather opinion on if this is best practice or I’m not using begin/rescue as it is intended?

  • 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-06T21:46:21+00:00Added an answer on June 6, 2026 at 9:46 pm

    DRYing this up is a great idea and a good lesson in rails design (and Test Driven Development/TDD) if you can do so.

    Ideally, you’d do something like this:

    def create
        ...
        grid = Tag.find_by_id(story[:tag_id]) or raise GridNotFoundError
        ...
        event = Event.find_by_id(story[:event_id]) or raise EventNotFoundError
        ...
      rescue GridNotFoundError
        flash.now[:error] = "Please select a category"
        process_grid_not_found(item, story, race, etc...)
      rescue EventNotFoundError
        flash.now[:error] = "Please select an event or create a new one if you don't find your event"
        process_event_not_found(item, story, race, etc...)
      rescue CreateEventError
        flash.now[:error] = "There has been a problem creating your event."
        process_event_create_error(item, story, race, etc...)
      rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound
        flash.now[:error] = item.errors.full_messages.join(" ,")
        process_other_error(item, story, race, etc...)
      end
      render 'home/race_updates'
    

    Then you should create the relavent new methods (process_event_not_found, etc) as separate (probably private) methods in the model.

    This both makes the code much more readable, but has the great advantage be being much easier to write test code for.

    So then you should write test code (using Test::Unit or rspec or whatever) that tests the isolated functionality required by each of the individual exception methods. What you’ll find is that this both yields better code, as well as it also will likely break-down the exception methods into smaller, more modular methods themselves.

    When you hear Ruby and Rails developers talk about the benefits of Test Driven Development, one of the main benefits of that approach is that it is much less likely to result in long, complex methods like the one you’ve got here. It’s much more likely that you’ll have code that is much DRYer, with smaller, simpler methods.

    I’d also recommend that once you get through this you take another look and try to simplify it further. There will be more room for simplification, but I’d recommend refactoring it iteratively and starting with breaking it down as I’ve described and getting tests in place to start.

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

Sidebar

Related Questions

I have this method call: public DataTemplate Create(Type type, string propertyName) { string str
I have a create method in one controller and at the end of this
I have this code working in C#: var request = (HttpWebRequest)WebRequest.Create(https://x.com/service); request.Method = GET;
I have this as my create method inside my PostsController class PostsController < ApplicationController
i have searched a lot. I'm facing this error Create method in Controller is
How to generate compressed files on request. I have this controller def create send_data
This is the code I have at the end a the create method in
I have this method: def update @user = User.find(params[:id]) respond_to do |format| if @user.update_attributes(params[:user])
Trying to use GridBagLayout. I have the method called buildLabel. This creates three label.
I have this: Create Proc CrearNuevoImagen @imagen image AS INSERT INTO Imagenes(imagen) VALUES( @imagen

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.