Topic: Scaffold

Hi all,

I have a product table containing id (auto increment and unique key), product_id, name, type

When I create a scaffold I can only enter the name. It seems that type and product_id are ignored (id I understand). Why is this? How can I overcome this?

Many thanks

Re: Scaffold

The "type" column is a reserved word, it is used for single table inheritence. If you are not using it for that, I recommend renaming it - maybe to "kind".

As for product_id, scaffolding doesn't set up associations as there are different ways to do it. However, here you have a product_id column in the products table? Do you want this to act like a tree?

Railscasts - Free Ruby on Rails Screencasts

Re: Scaffold

'kind' it will be then.

Indeed, I want it to be a tree. Does Rails handle this in a particular way?


Re: Scaffold

Rails recommend you call the column parent_id. You can use acts_as_tree to help you out.

As for creating a way to select the parent model, you can use collection select:

<%= collection_select :product, :parent_id, Product.find(:all), :id, :name %>

Of course, you will probably want to customize this so there's no way to select itself as a parent, or a child of the current product.

Railscasts - Free Ruby on Rails Screencasts