# Scaffold generator: create vs. new

**URL:** <https://www.ruby-forum.com/t/scaffold-generator-create-vs-new/56242>\
**Category:** Rails\
**Created:** [March 29, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/scaffold-generator-create-vs-new/56242 "2006-03-29T18:43:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scaffold\_generator](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@Scaffold\_generator](https://www.ruby-forum.com/u/Scaffold_generator)\
**Post date:** [March 29, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/scaffold-generator-create-vs-new/56242/1 "2006-03-29T18:43:00Z")

</div>

Can anyone supply me with a simple, succinct explanation of what the  
difference is between the “new” controller and the “create” controller  
that the scaffold generator produces?

I run:

> script/generate scaffold modelname controllername

And now I need to do some work inside the controllername.rb file. For  
example, pulling a list from another database table to supply for a  
drop-down select in the “create a new whatsit” form.

@sprockets = Sprocket.find(:all)

Do I put this in new or create? Why? I’ve been floundering around on  
this for some time, and I’ve not been confident that what I’m doing is  
the right way, is efficient as far as programmer hours, or will scale  
correctly in production.

Thanks all.  
–John

---

<div class="post-metadata">

**Author:** ![Scaffold\_generator](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@Scaffold\_generator](https://www.ruby-forum.com/u/Scaffold_generator)\
**Post date:** [March 29, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/scaffold-generator-create-vs-new/56242/2 "2006-03-29T19:08:33Z")

</div>

Scaffold generator: create vs. new wrote:

> @sprockets = Sprocket.find(:all)
> 
> Do I put this in new or create? Why? I’ve been floundering around on  
> this for some time, and I’ve not been confident that what I’m doing is  
> the right way, is efficient as far as programmer hours, or will scale  
> correctly in production.

The new and create methods implement _actions_ within the controller.

The new action is called after you choose “new”, and before the form is  
displayed to allow you to populate the new object. Note that the object  
created in the new method is there to keep the form rendering happy. If  
you set any values in that object, they will appear in the form.

The create action is called to take the values from that form, populate  
a new object, and save it in the database.

So to add information in the “create” form, you need to retrieve it in  
the new method.

regards

Justin

---

<div class="post-metadata">

**Author:** ![Scaffold\_generator](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@Scaffold\_generator](https://www.ruby-forum.com/u/Scaffold_generator)\
**Post date:** [March 29, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/scaffold-generator-create-vs-new/56242/3 "2006-03-29T20:30:19Z")

</div>

> The new action is called after you choose “new”, and before the form is  
> displayed to allow you to populate the new object. Note that the object  
> created in the new method is there to keep the form rendering happy. If  
> you set any values in that object, they will appear in the form.
> 
> The create action is called to take the values from that form, populate  
> a new object, and save it in the database.
> 
> So to add information in the “create” form, you need to retrieve it in  
> the new method.

Ah, so new is called before the form is displayed, and create is called  
when the form is submitted, right?

And the same relationship exists between the edit and update actions?  
Edit is called before the form is displayed, and update is called when  
it’s submitted?

Thanks, this helps a lot.

---

<div class="post-metadata">

**Author:** ![Scaffold\_generator](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@Scaffold\_generator](https://www.ruby-forum.com/u/Scaffold_generator)\
**Post date:** [March 29, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/scaffold-generator-create-vs-new/56242/4 "2006-03-29T22:06:00Z")

</div>

Steve K. wrote:

> Exactly. You’ve got it.
> 
> Just remember that the names don’t have any magical meaning; they’re  
> just the sensible names and the flows someone chose when creating the  
> scaffold generator. Your own actions and views can be called “turkey”  
> “gravy” and “cranberry\_sauce” if you want, though it’s probably best if  
> they make sense so you’ll understand your own code six months from now.

Yep. Funny that the scaffold generator gives these ambiguous default  
action names, when other Rails things are so much more obvious (like the  
various points that validation can be applied, for example.)

Thanks again.

---

<div class="post-metadata">

**Author:** ![Scaffold\_generator](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@Scaffold\_generator](https://www.ruby-forum.com/u/Scaffold_generator)\
**Post date:** [March 29, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/scaffold-generator-create-vs-new/56242/5 "2006-03-29T20:49:38Z")

</div>

Exactly. You’ve got it.

Just remember that the names don’t have any magical meaning; they’re  
just the sensible names and the flows someone chose when creating the  
scaffold generator. Your own actions and views can be called “turkey”  
“gravy” and “cranberry\_sauce” if you want, though it’s probably best if  
they make sense so you’ll understand your own code six months from now.

John wrote:

> > The new action is called after you choose “new”, and before the form is  
> > displayed to allow you to populate the new object. Note that the object  
> > created in the new method is there to keep the form rendering happy. If  
> > you set any values in that object, they will appear in the form.
> > 
> > The create action is called to take the values from that form, populate  
> > a new object, and save it in the database.
> > 
> > So to add information in the “create” form, you need to retrieve it in  
> > the new method.
> 
> Ah, so new is called before the form is displayed, and create is called  
> when the form is submitted, right?
> 
> And the same relationship exists between the edit and update actions?  
> Edit is called before the form is displayed, and update is called when  
> it’s submitted?
> 
> Thanks, this helps a lot.
