Plugins - db_column_with_spaces
Add to favoritesColumnWithSpaces
A simple plugin that enables ActiveRecord’s accessor methods and dynamic finders to work with legacy database tables that have column labels containing spaces. Include the module in your your model for a legacy database table and then you can substitute underscores for the column label spaces in your attribute accessor and dynamic find_by method calls.
Prerequisites
Tests only pass against Rails 2.0.2 at present. Rspec 1.1.3 was used to write the specs
Install
- git clone git://git.dentz.net/git/db_column_with_spaces.git vendor/plugins
- script/plugin install svn://svn.dentz.net/svn/plugins/db_column_with_spaces/trunk
Example
Given a record with following column labels and values:
first name: John
last name: Smith
middle_name: Kyle
Define a model for a legacy table:
class LegacyEmployee < ActiveRecord::Base
include DentzSinclair::ColumnWithSpaces
end
And now you can use underscores in finders and accessors.
>> @employee = LegacyEmployee.find_by_last_name('Smith')
=> #<LegacyEmployee id: 1, first name: "John", last name: "Smith", middle_name: "Kyle">
>> @employee.first_name
=> "John"
>> @employee.first_name = "Walter"
=> "Walter"
>> @employee.save
=> true
>> @employee
=> #<LegacyEmployee id: 1, first name: "Walter", last name: "Smith", middle_name: "Kyle">
Copyright © 2008 [Eirik Dentz Sinclair], released under the MIT license
Acknowledgements
Inspiration:
Testing and design:
http://svn.dentz.net/svn/plugins/db_column_with_spaces/trunk
http://svn.dentz.net/svn/plugins/db_column_with_spaces/trunk
Rails' (MIT)
Model
