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.
Written by Greg Osuri
Related protips
4 Responses
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#