f5mdoq
Last Updated: July 19, 2018
·
28.48K
· kn0tch

using belongs_to with scaffold generator to quickly create associations

Rails scaffold generators are great! you can use belongs_to to quickly add relations. In the below example, I have 2 models accounts and campaigns; campaigns belong to accounts.

$ rails generate scaffold account name:string
$ rails g scaffold campaign name:string account:belongs_to

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

4 Responses
Add your response

14959

Hadn't seen this documented anywhere else. Very helpful. Thanks!

over 1 year ago ·
16212

You can also use :references

over 1 year ago ·
22918

thanks! i'm just looking for this generator, is this in official doc?

over 1 year ago ·
25012

if i've made a model called User, the scaffold for his events would be
rails g scaffold Event name:string user:belongs_to ???? thx in advance

over 1 year ago ·