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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:11:58+00:00 2026-05-13T16:11:58+00:00

Let’s say I have a User model, and an Invoice model with a belongs_to

  • 0

Let’s say I have a User model, and an Invoice model with a belongs_to :user association.

Now I’m creating a new action for my InvoicesController, and the view that will be rendered. The view will have a select-element for selecting the user that this invoice will belong to.

So I need to fetch those users somewhere; my instinct is to leave this kind of thing out of the view. I end up with this:

def new
  @users = User.all
end

The form submit action is create. When the creation fails for some reason, I re-render the new action’s view.

def create
  invoice = Invoice.new params[:invoice]
  if invoice.save
    flash[:notice] = 'Invoice created!'
    redirect_to :action => 'show', :id => invoice.id
  else
    @users = User.all
    render :action => 'new'
  end
end

But as you can see, in order the re-render the new action, I have to fetch the users again.

This is just an example, but consider that I have some forms with several select-elements filled from the database, or similar constructs. That turns into an awful lot of repetition.

So how should I structure my controller in this situation?

  • Should I simply use User.all from my view?
  • Should I call new from within create to do the fetching for me?
  • Or something else?
  • 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-13T16:11:58+00:00Added an answer on May 13, 2026 at 4:11 pm

    For this I’d use a before_filter. For example you’d do something like:

      before_filter :fetch_all_users, :only => [:new, :create]
    
    protected
    
      def fetch_all_users
        @users = User.all
      end
    

    For 90% of my controllers I use the inherited resources plugin. It cuts down the amount of controller code you need to write for CRUD controllers, which also means you can cut down on the amount of tests you need to write.

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

Sidebar

Related Questions

Let's say I have an facebook application running using the JS SDK. First user
Let's say I have a method in java, which looks up a user in
Let's say we have this code: <form action='' method='POST' enctype='multipart/form-data'> <input type='file' name='userFile'><br> <input
Let's say i have two tables in db: Car and Part. Car owns arbitrialy
Let’s say I have a number like 0x448 . In binary this is 0100
Let's say I have a 12-bit Analog to Digital Converter (4096 bins). And let's
Let's say I have a structure named vertex with a method that adds two
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have window.open (without name parameter), scattered in my project and I

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.