# Newbie question: getting scaffolding to handle belongs\_to re

**URL:** <https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567>\
**Category:** Rails\
**Created:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567 "2006-11-20T14:46:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/76d3ee/32.png) [@DavidB](https://www.ruby-forum.com/u/DavidB)\
**Post date:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567/1 "2006-11-20T14:46:28Z")

</div>

Sorry in advance if this question is very simple. I was looking for  
advice or a link to help with the following issue.

Let’s say I have an app where I have categories of food like  
‘Meat’,‘Dairy’,‘Fruit’ and the MVC functionality of this works well  
already.

Now I create another sub-application where I add certain items of food  
like ‘Orange’,‘Chicken’,‘Milk’, but I want to allow a user to pick  
which category a particular item belongs to. Is there an automated way  
of getting such an html select box with categories displayed?

BTW, I have successfully done this already, but I am afraid that I am  
not harnessing the potential of RoR entirely here.

THANKS IN ADVANCE,  
David

My sequence:

1. create tables
2. create models
3. create scaffolding
4. modify items\_controller
5. modify \_form.html for items to allow for an html select to be  
created

Please offer any alternatives to what I have already done below:

## tables= categories, items –each have their own id –items also has category\_id

## class Item \< ActiveRecord::Base belongs\_to :category validates\_associated :category end

class ItemsController \< ApplicationController  
def index  
list  
render :action =\> ‘list’  
end

def list  
@Item\_pages, @Items = paginate :Item, :per\_page =\> 10  
end

def show  
@Item = Item.find(params[:id])  
end

def new  
@categories=Category.find\_all  
@Item = Item.new  
end

def create  
@Item = Item.new(params[:Item])  
if @Item.save  
flash[:notice] = ‘Item was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
@categories=Category.find\_all  
render :action =\> ‘new’  
end  
end

def edit  
@categories=Category.find\_all  
@Item = Item.find(params[:id])  
end

def update  
@Item = Item.find(params[:id])  
if @Item.update\_attributes(params[:Item])  
flash[:notice] = ‘Item was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @Item  
else  
render :action =\> ‘edit’  
end  
end

## def destroy Item.find(params[:id]).destroy redirect\_to :action =\> ‘list’ end end

\_form.rhtml  
\<%= error\_messages\_for ‘item’ %\>

Item  
\<%= text\_field 'item', 'item' %\>

Description  
\<%= text\_field 'item', 'description' %\>

\<%=options\_from\_collection\_for\_select @categories, "id", "category",@item.category\_id%\>

Created on  
\<%= datetime\_select 'item', 'created\_on' %\>

Updated on  
\<%= datetime\_select 'item', 'updated\_on' %\>

---

<div class="post-metadata">

**Author:** ![DavidB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/76d3ee/32.png) [@DavidB](https://www.ruby-forum.com/u/DavidB)\
**Post date:** [November 20, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567/2 "2006-11-20T18:41:32Z")

</div>

On 11/5/06, DavidB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> which category a particular item belongs to. Is there an automated way  
> of getting such an html select box with categories displayed?

Check out the Scaffolding Extensions plugin:  
[http://wiki.rubyonrails.com/rails/pages/Scaffolding+Extensions+Plugin](http://wiki.rubyonrails.com/rails/pages/Scaffolding+Extensions+Plugin)

Jeremy

---

<div class="post-metadata">

**Author:** ![DavidB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/76d3ee/32.png) [@DavidB](https://www.ruby-forum.com/u/DavidB)\
**Post date:** [November 20, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567/3 "2006-11-20T19:31:42Z")

</div>

This question comes up a lot. The correct answer is that scaffolding is  
not  
meant to do that… it’s to get you started, not to build your app for  
you.

[http://blog.nicksieger.com/articles/2006/06/23/railsconf-amy-hoy-scaffolding](http://blog.nicksieger.com/articles/2006/06/23/railsconf-amy-hoy-scaffolding)

---

<div class="post-metadata">

**Author:** ![DavidB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/76d3ee/32.png) [@DavidB](https://www.ruby-forum.com/u/DavidB)\
**Post date:** [November 21, 2006, 1:47am UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567/4 "2006-11-21T01:47:00Z")

</div>

On 11/20/06, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This question comes up a lot. The correct answer is that scaffolding is not  
> meant to do that… it’s to get you started, not to build your app for you.

I wouldn’t say that’s the “correct answer.” It is the answer given by  
Rails core members and certainly one of Rails opinions (this is the  
reason it is called “scaffolding” in Rails). This “answer” is not  
necessarily shared by others outside of the Rails community. For  
example, in Django, usable production “scaffolding” is one of the main  
features (it’s called the “Automatic admin interface”).

In my opinion, the main reason that the Rails community generally  
frowns upon usable scaffolding is that Rails is primarily aimed at web  
application production, whereas usable scaffolding mainly benefits  
content site production (which is what Django is aimed at). As I  
mainly administer content sites, usable production scaffolding saves  
me a large amount of time. I use it on all of my sites, even ones  
that are more application than content.

I do object to the idea that usable production scaffolding is  
inherently bad. In fact, I can’t see any reason why you wouldn’t want  
scaffolding to be as good as it can be, even if you then go on to  
modify it.

For a lot of people, usable production scaffolding is a good thing.  
That’s why there are a lot of plugins/generators devoted to it. I’m  
not saying usable production scaffolding should be a core feature, as  
it is in Django. However, I think that usable production scaffolding  
doesn’t deserve the scorn that it often receives in the Rails  
community.

Jeremy

---

<div class="post-metadata">

**Author:** ![DavidB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/76d3ee/32.png) [@DavidB](https://www.ruby-forum.com/u/DavidB)\
**Post date:** [November 21, 2006, 4:35am UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567/5 "2006-11-21T04:35:18Z")

</div>

@Jeremy:

Good points, all of them.

Scaffolding works for small sites. It works for the simple cases. When  
you  
start getting into situations where you have lots of relationships,  
multiple  
models per form, etc, it’s not going to cut it and you’re better off  
just  
writing it yourself because it’s not that hard.

THe main problem I have with scaffolding is that people see it and  
expect to  
be able to write big applications because it looks so easy. There are  
tons  
of things in the scaffolding that you just wouldn’t want in any  
production  
application, like the loops within loops, the lack of eager loading, and  
the  
pagination that really doesn’t work with large resultsets.

To me, scaffolding is something the newbies should use to see how things  
work in Rails. I think it’s a neat excercise to see people writing  
their  
own scaffolding extensions, but I’ve seen time and time again that  
generic  
solutions just require too much work to modify to meet the current  
situations. That’s why I like the fact that scaffolding in Rails does  
very  
little 🙂

All good stuff though… thanks again for the thoughtful reply!

---

<div class="post-metadata">

**Author:** ![DavidB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/76d3ee/32.png) [@DavidB](https://www.ruby-forum.com/u/DavidB)\
**Post date:** [November 21, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567/6 "2006-11-21T18:20:07Z")

</div>

On 11/20/06, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Scaffolding works for small sites. It works for the simple cases. When you  
> start getting into situations where you have lots of relationships, multiple  
> models per form, etc, it’s not going to cut it and you’re better off just  
> writing it yourself because it’s not that hard.

Scaffolding works best for small sites, though it works fine for large  
sites (using autocomplete for large tables). It’s true that it  
doesn’t work with complex things like multiple models per form, but  
lots of relationships isn’t a problem. Scaffolding is never going to  
maximize the productivity when using a form, only when building it.  
Scaffolding doesn’t solve hard problems, only tedious ones.

> THe main problem I have with scaffolding is that people see it and expect to  
> be able to write big applications because it looks so easy. There are tons  
> of things in the scaffolding that you just wouldn’t want in any production  
> application, like the loops within loops, the lack of eager loading, and the  
> pagination that really doesn’t work with large resultsets.

True, scaffolding makes things look easy. That’s because using  
scaffolding is easy, and if it solves your problems, there isn’t a  
reason to do anything more difficult.

It’s also true that scaffolded code generally performs poorly.  
However, it can handle eager loading, and performance of the  
scaffolded forms themselves may not be an issue. It can be used as an  
administrative front end to a high traffic site (particularly if  
updates aren’t that frequent), though I would recommend custom coding  
any forms that themselves get a lot of traffic.

> To me, scaffolding is something the newbies should use to see how things  
> work in Rails. I think it’s a neat excercise to see people writing their  
> own scaffolding extensions, but I’ve seen time and time again that generic  
> solutions just require too much work to modify to meet the current  
> situations. That’s why I like the fact that scaffolding in Rails does very  
> little 🙂

Generated scaffolding is useful as a teaching tool, but I find method  
based scaffolding makes for easier maintenance. I find that if  
scaffolding doesn’t meet your current needs, it’s better to extend  
it’s capabilities than it does repeating yourself making small  
modifications to a whole bunch of forms. In fact, I think the more  
models you have, the more scaffolding is beneficial. Writing or  
modifying CRUD forms for a 3 model site isn’t bad, but it’s pretty  
mind numbing for an 100 model site.

Again, I think a lot of it depends on your needs. If you are writing  
a complex web app with a lot of specialized forms, then it won’t help  
much. If you are looking for an administrative front end to a content  
site, it does the job quite well.

Jeremy

---

<div class="post-metadata">

**Author:** ![DavidB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/76d3ee/32.png) [@DavidB](https://www.ruby-forum.com/u/DavidB)\
**Post date:** [November 22, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567/7 "2006-11-22T19:51:34Z")

</div>

Thanks to all of you. As I look back at my question, I have already  
learned how to do this and have learned what to expect from  
scaffolding. Hopefully, other questions from me will challenge you  
more, and hopefully I will soon contribute as well.

Thanks-David

---

<div class="post-metadata">

**Author:** ![DavidB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/76d3ee/32.png) [@DavidB](https://www.ruby-forum.com/u/DavidB)\
**Post date:** [November 21, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/newbie-question-getting-scaffolding-to-handle-belongs-to-re/76567/8 "2006-11-21T15:32:26Z")

</div>

Yes, my advice would be to just spend a little time using scaffolding to  
orient yourself with some of the rails basics, then just move on. I  
remember watching that blog on scaffolding screencast, and thinking it  
looked so easy. But as soon as I started digging into the scaffold code  
(without knowing much ruby or rails) I quickly got lost.

If you really need something quick and dirty, go with scaffolding. But  
if you need anything beyond the default interface, or if you’re trying  
to learn Rails, spend some time studying the scaffold code, then go  
build your own. I don’t think the default code is worth modifying, and  
you’ll be happier with (and prouder of) a custom solution.
