Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Changing the column name without having to modify a bunch of other code


  • Please log in to reply
7 replies to this topic

#1 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 02 June 2014 - 06:45 PM

I made this migration which did change the column name, but I found I had to rewrite a bunch of view, model and other code to reflect this name change.

 

class ChangePersonStatusToPersonGroup < ActiveRecord::Migration
  def change
    rename_column :people, :status, :category
  end
end

 

I created person using scaffolding. what is the better way to change that column name without also having to manualy search for status and Status and change them to category and Category in

app/views/people/show.html.erb
app/views/people/_form.html.erb
app/views/people/index.html.erb
and a few other files.

 

I'm using rails 4 and ruby 2. Let me know if there is any other info about my setup, or other clarification you need. Any help is appreciated. :)



#2 Ohm

Ohm

    Driver

  • Moderators
  • 452 posts
  • LocationCopenhagen

Posted 03 June 2014 - 08:55 AM   Best Answer

Unfortunately, no. 

 

The reason here is quite obvious. What if I created app/views/people/show.html.erb with scaffold and then changed it totally afterwards, then how should the scaffold know it should change stuff back?

 

That being said, some IDEs like RubyMine (http://www.jetbrains.com/ruby/) can do refactoring for you - or at least help you with it.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#3 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 03 June 2014 - 07:56 PM

Wow, really? What is the standard way of making your own script that does this kind of thing for you? I would normally write a bash script but if there's a baked-in/standar rails or ruby way I'd prefer to do that.



#4 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 03 June 2014 - 08:02 PM

Also, IDE's bah! :P vim all the way!



#5 Ohm

Ohm

    Driver

  • Moderators
  • 452 posts
  • LocationCopenhagen

Posted 04 June 2014 - 04:55 AM

Nope, no standard Rails (or Ruby) way of doing this, not that I know of.

 

You could just use 

:s/status/category/g
:s/Status/Category/g

in your vim.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#6 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 04 June 2014 - 03:42 PM

That is really disappointing. I would think that at the very least the model and controller code could be refactored. I think I'll write a bash script the next time I have to do this.



#7 Ohm

Ohm

    Driver

  • Moderators
  • 452 posts
  • LocationCopenhagen

Posted 04 June 2014 - 04:04 PM

I'm interested in why you find this disappointing. Can you name an example of where you've seen this type of functionality before, without it being part of an IDE?


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#8 th3mus1cman

th3mus1cman

    Signalman

  • Members
  • 14 posts
  • LocationRichmond, VA

Posted 04 June 2014 - 05:25 PM

There are too many edge cases that make this type of functionality not worth the time to develop when it comes to static code generation.  Something like ActiveAdmin is better for the type of auto updating functionality you are looking for, but it is only really suited to admin dashboard type of stuff.


Rails Developer and Partner at Left + Right





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users