Relationships between tables issue

Hi, there:    I made two models - "task" and "comment" in my app without any connections each other, but now I wanna a "one to many" between them, saying one task has many comments, one comments has to belong to one task.   Therefore, I add "belongs_to :task" and "has_many :comments" in each model respectively.

The question is:   1> I dont think the two tables in the database will add the foreign_key column inside automatically after I said "belongs_to & has" in models. Am I right? At least I didnt find it by investigating database tables.   2> what should I do if I dont wanna use SQL or DDL to create foreign_key cloumn, is the "migrate" able to help me?

Thanks a lot! Myst

myst_tt wrote:

Hi, there:    I made two models - "task" and "comment" in my app without any connections each other, but now I wanna a "one to many" between them, saying one task has many comments, one comments has to belong to one task.   Therefore, I add "belongs_to :task" and "has_many :comments" in each model respectively.

thats good and how it should be done

The question is:   1> I dont think the two tables in the database will add the foreign_key column inside automatically after I said "belongs_to & has" in models. Am I right? At least I didnt find it by investigating database tables.

you need to add the foreign keys yourself. rails is not doing it for you. if you stick to the convention that the foreign key is named id_classname then it will be picked up by the relationshsip automatically .. otherwise you will need to specify it explicitely in the code

  2> what should I do if I dont wanna use SQL or DDL to create foreign_key cloumn, is the "migrate" able to help me?

yep, use a migration to add the column you need

Thank you!