Reflect table changes in models

Hello,

I am adapting an existing system and I want to change some tables. I want to turn a column into a reference to a new table. What's the best way to do this? I guess migrations but how do I make the new models then? I am pretty new to rails so if you could provide me with the generator commands to that would be great. Of course online references are fine too!

Regards, Bart

Hi Bart,

Bart Braem wrote:

how do I make the new models then?

It seems unlikely, for someone undertaking a change to an app's data model, that what you're looking for is this basic, but just in case... To generate a new model for a table:

ruby script\generate model ModelName

Also, don't forget the Rails convention is that the ModelName is the singular of the table's name.

hth, Bill

I am adapting an existing system and I want to change some tables. I want to turn a column into a reference to a new table. What's the best way to do this?

If you have existing models, they will pick up any changes to the database.

For example, if you a 'products' table with an associated 'Product' model:

# assign price product.price = 5

Then you decide to change 'price' to 'sale_price' and generate/run a migration to do so. You can then just use the new column without changing your model:

# assign price product.sale_price = 5

Hope that helps and sorry if it's not quite what you meant :0)

Steve

Stephen Bartholomew wrote:

I am adapting an existing system and I want to change some tables. I want to turn a column into a reference to a new table. What's the best way to do this?

If you have existing models, they will pick up any changes to the database.

For example, if you a 'products' table with an associated 'Product' model:

# assign price product.price = 5

Then you decide to change 'price' to 'sale_price' and generate/run a migration to do so. You can then just use the new column without changing your model:

# assign price product.sale_price = 5

Hope that helps and sorry if it's not quite what you meant :0)

It's not really what I meant. I'll give an example with the product table. I currently have price, but say that I want to be a row in another table pricetag. That row would have columns like tagcolor, currency and value. (As an example, I don't need currency conversion!) So how do I make that conversion to a reference to an external table? If I generate the table with migrations it seems to fail...

Regards, Bart