Better legacy database support for Rails

Have you seen these errors?

NoMethodError: undefined method `columns_hash' for "":String
NoMethodError: undefined method `private_method_defined?' for "":String

If you’ve tried using Rails 3 with a legacy database, it is possible you’ve run across issues with some of your columns having the same name as existing methods of instances of ActiveRecord::Base. My primary example of this has been columns with the name ‘class’. If you can’t change the column name to be ‘klass’ or ‘clazz’, then you may want to consider my solution to this issue.

I’ve created a gem called safe_attributes that turns off the creation of the attribute accessors that conflict with existing instance methods of ActiveRecord::Base.

https://github.com/bjones/safe_attributes
https://rubygems.org/gems/safe_attributes

About these ads
This entry was posted in rails and tagged . Bookmark the permalink.

2 Responses to Better legacy database support for Rails

  1. Pingback: Legacy Column Names in Rails 3 « Kansas City on Rails

  2. Great article, Bryan! I expanded on your post in my own blog (giving you credit, of course), with example code and a downloadable sample app:

    Legacy Database Column Names in Rails 3

    More importantly, thank you for digging in and creating a nice solution to this problem. I’m sure it’ll help a lot of people, and might be worth submitting to the rails core itself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s