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!

Re: Nested tables

Lets get your model relationships right first.

Your user needs either a has_one :address or a has_many :addresses depending on whether or not you want to have multiple addresses
You may also want an STI solution for addresses to have types of home, work etc... but that can happen later.
Your address
  belongs_to :user
  belongs_to :postal_code

Your post_code
  belongs_to :district
  belongs_to :country

Your district
  has_many :postal_codes
  belongs_to :country

your country
  has_many :districts

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

I think you'll find that what you really want is
Users.firstname should be user.first_name (You would be referencing an instance of the class not the model and also not an array of instances. You seem to be a little mixed up with your naming conventions) remeber the underscore between words as well. Rails is all about convention over configuration

Users implies that you are referencing many classes. I'm sure you meant user to be a reference to a single instance of an object
and users would be referencing an array of objects so the firstname property would not be available on that

a label on your form using rails helpers for

<%= f.label :firstname %>:

would visually produce

Firstname:

Where as you would want

First name:

This is what the _ between first and name will give you and obviously you will need the column in your db to reflect this naming convention.

User.lastname should be user.last_name for the same reason as above
A capital U also means you are referencing a class rather than an object. I'm sure you didn't mean that.

Addresses.address should be user.address.house (or whatever fields you have on the address table) You wouldn't reference the Address class for an address property.
and so on

To use the structure I gave you above you would do something like

 @user = User.find(some_id_param)
 @user.address.postal_code.postal_code
 @user.address.postal_code.district
 @user.address.postal_code.country

That should do for now

When your head is hurting from trying to solve a problem, stop standing on it.
Then when you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)