在他们的轨道和碎activemodel的以#列名 [英] column names with # in them rails and broken activemodel

查看:70
本文介绍了在他们的轨道和碎activemodel的以#列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有喜欢LYEAR#2,#LYEAR 3(可怕的)列名的遗留表... LYEAR#9

I have a legacy table with (horrible) column names like LYEAR#2, LYEAR#3 ... LYEAR#9

我建立一个模型,它名为Glamas

I setup a model for it named Glamas

如果我有:

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

这是与activemodel的一个问题,或者是它的东西,我可以用的东西在我的code修复?

Is this a problem with activemodel, or is it something that I can fix with something in my code?

这是轨道3 BTW。

#glamas.rb
class Glamas < ActiveRecord::Base
  establish_connection "turnkey"
end

#database.yml
turnkey:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: turnkey
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

我在两岸从旧机牵引的表,我想(如果我是绝对必须的),我可以重新命名列时,我拉不动了,但proess已经是一个巨大的痛苦和处理器密集型的。

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.

推荐答案

你不会是能够使用这些表与活动记录。 AR定义实例方法不在方法名允许你的类而被命名为相同的列和红宝石(或任何与此有关的名字,因为它是评论字符)。

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.

这篇关于在他们的轨道和碎activemodel的以#列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆