f5mdoq
Last Updated: March 19, 2017
·
22.58K
· kn0tch
9c6165b107059ea5dfa2e81985fe8272

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.

Say Thanks
Respond

4 Responses
Add your response

14959
3be717488d1843d97eba0f9892b3be7d

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

over 1 year ago ·
16212
01e524cc2a4c9ee03a4cf982d6683adf

You can also use :references

over 1 year ago ·
22918
None

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

over 1 year ago ·
25012
335dc71d8aed6e2216c13a7b3800ca86 normal

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 ·
Awesome Job

0dbe43fc 55f1 11e7 9f7e e393aaf472a4
Web Developer
·
Long Beach, CA
·
Full Time