Search code examples
ruby-on-railsrubyscaffoldingscaffold

Linking two tables generated from "rails generate Scaffold" (New to Ruby on Rails)


I would like to save a person's information like Education, Address, etc into database tables.

If I generate Person Table(with Education) using Scaffold - id is automatically generated as primary key.

I would like to generate another table called Address for the same person using Scaffold. How can I link the id of Address table to id of Person table?


Solution

  • You want person to have_many addresses -see the rails guide for more on associations http://guides.rubyonrails.org/association_basics.html#the-has-many-association

    You can scaffold this by doing

    Rails g scaffold Address person:references line1:string city:string etc
    

    This will generate another model, Address, with a foreign key person_id to say which person it belongs to.