开发者

How do I recover from a botched ActiveRecord model rename?

I created a Rails model (and controller) with a typo in the name. I renamed all of the files to the correct names, and then I rolled back the migration I used to create the table and changed it to recreate the table with the proper name.

Unfortunately, ActiveRecord still wants to use the old table name, even though it does not appear in any file in my project. I assume it has been cached somewhere but I have no idea where. There are no files in the application's tmp directory that look suspicious.

开发者_开发知识库For the time being, I added a call to "set_table_name" to the model to get around the problem, but I'm really curious about where the old table name is stored and how to get rid of it.

Update: I went ahead and deleted the scaffold using "rails destroy scaffold". When I recreated it (without the typo), it recreated everything with the typo! I know the typo is cached somewhere but I have no idea where.


Rafe - Looks like it could be a bug in Rails. Perhaps you could submit a Rails pull request, or try adding to the config/initializers/inflections.rb file.


have you fixed the class name of your model? rails infers the table name from that

e.g. "class Userr" -> "userrs"


I generally spot typos quite quickly: the first time that the model is mentioned in the console or associations; the controller in the routes.

When I rails generate model urser I just rails destroy model urser and start again.

This just blasts the files away, but it's very convenient and in rails 3 works especially well to destroy every file created by the generator.

If I migrated before spotting the typo I'll let the migration be deleted by the destroy script, let the generate write a new one and then rake db:rollback. That way the urser_table from the previous migration is dropped and the user_table is created.

If there's a bit of code in the files, at that point is mostly in the model or controller itself. I just copy to clipboard the meat of the class before deleting the file and I paste it in the next one.

If there's a lot of code inside various models tests, controllers or helper files: I still use the same approach, but commit it to git before running destroy, so if you something it can always be checked back out.


OK, it turns out that with Rails 3 (and maybe other versions) if you try to generate a model named "Cafe" it will use the name "cave" instead. No idea why.

Here's an example. I duplicated this on different computers, too.

holloway:whatever rafeco$ rails g scaffold Cafe 
  invoke  active_record
  create    db/migrate/20110412190231_create_caves.rb
  create    app/models/cafe.rb
  invoke    test_unit
  create      test/unit/cafe_test.rb
  create      test/fixtures/caves.yml
   route  resources :caves
  invoke  scaffold_controller
  create    app/controllers/caves_controller.rb
  invoke    erb
  create      app/views/caves
  create      app/views/caves/index.html.erb
  create      app/views/caves/edit.html.erb
  create      app/views/caves/show.html.erb
  create      app/views/caves/new.html.erb
  create      app/views/caves/_form.html.erb
  invoke    test_unit
  create      test/functional/caves_controller_test.rb
  invoke    helper
  create      app/helpers/caves_helper.rb
  invoke      test_unit
  create        test/unit/helpers/caves_helper_test.rb
  invoke  stylesheets
  create    public/stylesheets/scaffold.css
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜