Do I Edit the Migration Too to Show the Belongs To

Hi Everyone,

Do I edit the corresponding migration also to show the belongs_to (and the has_many)? I have a many to many relationship where I am using the “has_many :through” association. When I place in the individual classes the ‘has_many :through’ and the ‘belongs_to’ associations, do I also edit the corresponding migrations to reflect those changes?

I would like those changes to be reflected in the database also, but I don’t know whether there was a way to run one of the migration generator commands that would have included these associations.

Also, I have already run the migration so is there a way to create a new migration that would simply add these associations? I would prefer not to edit existing migrations.

Thank you in advance for the advice. I’m very new to Rails and am just learning it with a toy application I’m building.

p

Hi Everyone,

Do I edit the corresponding migration also to show the belongs_to (and the has_many)? I have a many to many relationship where I am using the "has_many :through" association. When I place in the individual classes the 'has_many :through' and the 'belongs_to' associations, do I also edit the corresponding migrations to reflect those changes?

No, there is no need to change the existing migrations. The relationship declarations (has_many and so on) have not direct effect on the database contents, they just tell Rails what to expect in the database.

I would like those changes to be reflected in the database also, but I don't know whether there was a way to run one of the migration generator commands that would have included these associations.

Also, I have already run the migration so is there a way to create a new migration that would simply add these associations? I would prefer not to edit existing migrations.

Whenever you make a change to the database use a new migration. You will just get yourself (and likely the database) very confused if you try to add to an existing migration.

Thank you in advance for the advice. I'm very new to Rails and am just learning it with a toy application I'm building.

Work right through a good tutorial such as railstutorial.org (which is free to use online) before starting you own experiments. That will show you the basics of Rails.

Colin