Newbie question: getting scaffolding to handle belongs_to relationships

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

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

Jeremy

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

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

@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 :slight_smile:

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

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 :slight_smile:

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

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