Topic: Database structure planning

Hi,

I'm having a hard time setting up the basic structure of my databases.

I have products (about 50). Each products is related to one or more place(s).

The basic schema would be this (no relation yet)

Products
id:integer
name:string

Places
id:integer
name:string
content:string

How can I connect them two?

Last edited by brainy (2012-03-05 15:13:45)

Re: Database structure planning

Product.rb

has_and_belongs_to_many :places

Place.rb

has_and_belongs_to_many :products

Create a new db migration and name the table corresponding to the 2 models being linked, and write them alphabetically:

rails generation migration places_products place:references product:references

Rake the DB migration

rake db:migrate

Now, products and places can be linked to one another. Each product and/or place can have multiple (or none) of it's relation model.

I hope I understood your problem correctly. For more help, research "has and belongs to many" relationships in Rails. Or, basic relationships as well. Read up on those and let me know if you have anymore questions

Last edited by wesf90 (2012-03-06 18:26:06)

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: Database structure planning

Worked perfectly! Thank you very much.

Re: Database structure planning

brainy wrote:

Worked perfectly! Thank you very much.

No problem, glad I could help!

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!