column names with # in them rails and broken activemodel
I have a legacy table with (horrible) column names like LYEAR#2, LYEAR#3 ... LYEAR#9
I setup a model for it named Glamas
If I have:
accounts = Glamas.all
account_mains = accounts.collect(&:ACCOUNT_MAIN)
It breaks with this error:
SyntaxError: compile error
/usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:273: syntax error, unexpected kUNDEF, expecting ')'
undef :LYEAR#9?
^
/usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:274: syntax error, unexpected kEND, expecting $end
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:274:in `define_attribute_methods'
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `each'
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `define_attribute_methods'
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `each'
from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `define_attribute_methods'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:13:in `define_attribute_methods'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:41:in开发者_开发百科 `method_missing'
from (irb):31:in `to_proc'
from (irb):31:in `collect'
from (irb):31
from :0
Is this a problem with activemodel, or is it something that I can fix with something in my code?
This is rails 3 btw.
#glamas.rb
class Glamas < ActiveRecord::Base
establish_connection "turnkey"
end
and...
#database.yml
turnkey:
adapter: mysql
encoding: utf8
reconnect: false
database: turnkey
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
I am pulling the tables strait over from the legacy machine, I guess (If I absolutely have to) I could rename the columns when I pull them over, but the proess is already a huge pain and processor intensive.
You're not going to be able to use these tables with Active Record. AR defines instance methods for your classes which are named the same as the columns and in ruby #
isn't allowed in method names (or any names for that matter as it's the comment character).
I'd suggest either creating views in the database with more sensible names or just renaming the columns.
精彩评论