开发者

What is a good way to change some hard-coded files to a active-record models?

Right now my products model has a string column of category, with the form having a select which gets its values from an array in the product model. Right now there are only three possible categories: wood_stoves, arborist_gear, and chainsaw开发者_JAVA百科s. I have a controller with an action for each of those that lists the products for their respective category.

I'm switching the categories to a database table and nesting products in it with has_many and belongs_to relationships, and here's my question. Is the following a bad idea?

  1. Create a record for each of wood_stoves, arborist_gear, and chainsaws.
  2. Create a migration that converts the value of each of those in the products/category column into the corresponding record id integer from the newly created categories records.
  3. Add to the migration a step that changes the category.string column to a category.integer column.
  4. Add to the migration the necessary down steps to revert all that.

In short, should I do all this in a migration so it's reversible, or should I abandon reversibility and manually make the changes to the database to avoid a somewhat ridiculous looking migration that will only be able to reverse products that correspond to the original categories? In other words, the migration won't be able to reverse products that belong to newly created categories.


Sounds good. The only thing that I would do different is stick with the Rails convention of having a foreign key column name end with "_id". Life is just easier that way. In your case remove the category column from your products model and add a category_id column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜