8

i'm starting now on Rails, i looked in the forum, but i didn't find anything that could solve my problem.

Here it goes, I have a Category table, and it has only name for a column (there is no repetition in categories) so i would like name to be the primary key, then i have a Product table that has name, main_photo, description and i would like to say that a product only has a category, do i need to add a column named category as a foreign key in products?

A Category is suposed to have many products.

Then in category models how do i say that name is the primary Key, and how can i do the correspondence between the suposed primary key name in categories and category in products?

oberfreak
  • 1,799
  • 13
  • 20
Márcio Duarte
  • 453
  • 3
  • 5
  • 14

2 Answers2

15

Foreign key constraints in Active Record aren't used very often as the ideology behind Active Record says that this kind of logic should belong in the model and not in the database - the database is just a dumb store: http://guides.rubyonrails.org/migrations.html#active-record-and-referential-integrity.

The Rails way is to have an ID column on all tables including your Categories table, and in your Products table, have a column called Category_ID. Notice that the table names are plurals.

Then in your model you define the relationships between the entities Product and Category. Read the article A Guide to Active Record Associations and it will answer all your questions, especially sections 2.1, 2.2 and 3.3.

dnatoli
  • 6,972
  • 9
  • 57
  • 96
  • But when i do rails generate scaffold Category name:string, doesn't it create an id column by default? my table will have then a id and a name column.. – Márcio Duarte Dec 01 '11 at 13:59
  • 1
    Yes, it should add an ID column to each table. But it won't add a Category_ID columns to the Products table, you'll have to do that manually. – dnatoli Dec 01 '11 at 22:47
  • 1
    Read through the link I posted in my answer, all the answers to your questions are there. – dnatoli Dec 01 '11 at 22:54
  • 5
    It can add a category_id automatically if you generate your migration with category:references as an argument, or just add `t.references :category` to the migration. – Sean Hill Dec 02 '11 at 05:45
  • but it says to create a corresponding id column in a migration. that doesn't make tables dumb anymore. they're linked via those foreign keys. Am i mistaking the definition of 'dumb tables'? maybe the only one that is dumb is me. lol – ahnbizcad Apr 24 '14 at 12:01
  • 1
    I meant dumb store as in there is no referential integrity or business logic (i.e constraints) in the database. However, this doesn't mean that you shouldn't add these later. The reason they can't be done in active record is that it is meant to be database agnostic, and in some databases you can't add foreign keys. So if you can add them, you should. – dnatoli Apr 30 '14 at 20:24
6

There are many valid reasons to have foreign keys in your database. See Does Rails need database-level constraints?

I recommend Foreigner if you want to easily add foreign keys to your Rails app.

Community
  • 1
  • 1
tee
  • 4,149
  • 1
  • 32
  • 44