Last Updated: July 26, 2022
·
45.62K
· 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

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

over 1 year ago ·

You can also use :references

over 1 year ago ·

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

over 1 year ago ·

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 ·