Topic: Nested tables
Hello,
I have been unable to create a solution to my problem. I have found some resources, but still the solution eludes me. Hopefully you can help me.
I have a register page with basic information such as name, etc.
It has a field for Address, Postal Code, District and Country.
The table structure is as follows:
The Addresses table has a user_id and a postal_code id.
The Postal_Codes table has a District column and a country_id column (which references the Countries table)
When someone registers i want to write the information into the Users table of course.
I want to write the address into a new row in the Address table and also query the Postal_Codes table and get an id.
(the postal_codes and countries tables will be properly populated of course)
When i want to retrieve this data i want to retrieve the Users.firstname and User.lastname, the Addresses.address, Postal_Codes.postal_code, Postal_Codes.district and Countries.country
SQL-wise i could write this. But seeing as i am fairly new to ruby on rails i didnt manage it.
Right now i have tried to set up the models with various belongs_to, has_many, through, accepts_nested_attributes_for, etc. But i cannot create the proper solution and i would very much appriciate a lesson in this.
Can anyone point me in the right direction? How to set up the models and controllers to access this data properly.
Thank you for reading!