Yu-Chieh’s Blog (Y.C. Chang)

Ruby on Rails / Rubygems / FullStack / Git / Mac notes.

Rails Tips: Rails G Scaffold for Reference Field

Rails scaffold generators are very smart! you can use belongs_to to quickly add relations. In the following example, I have 2 models products and product_types; Product_types belong to product and product has many product types.

1
2
3
4
5
6
7
8
9
$ rails g scaffold product name:string
$ rails g scaffold product_type name:string product:belongs_to

# You can also use :references
$ rails g scaffold product_type name:string product:references

# For more manually work, you can do it like this
$ rails g scaffold product_type name:string product_id:integer
# then add "belongs_to" in your ProductType model

This will generate the necessary columns, indexes and associations in your models.

Comments