我怎样才能永久地忽略我的ActiveRecord :: Base类的数据库列? [英] How can I permanently ignore a database column in my ActiveRecord::Base class?

查看:192
本文介绍了我怎样才能永久地忽略我的ActiveRecord :: Base类的数据库列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的数据库,我想用Rails模型。其中一个表中有一个名为列属性,这是由Rails的我想保留的名称。

I have a legacy database which I'm trying to model using Rails. One of the tables has a column named attributes, which is a name reserved by Rails I think.

下面是SQL的表:

CREATE TABLE `album` (
  `id` int(11) NOT NULL,
  `artist` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `gid` char(36) NOT NULL,
  `modpending` int(11) DEFAULT '0',
  `attributes` int(11) DEFAULT '0',
  ...
);

下面是我的ActiveRecord类:

Here's my ActiveRecord class:

class Album < ActiveRecord::Base
  set_table_name "album"
  belongs_to :artist
  has_many :tracks, :through => :album_tracks
end

下面是当我尝试实例化一个实例会发生什么:

Here's what happens when I try to instantiate an instance:

hornairs@bishop:~/Sites/logdb (master *)$ rails c
Loading development environment (Rails 3.0.3)
no such file to load -- irbtools
ruby-1.9.2-p0 > x = Album.find_by_name("Champ")
 => #<Album id: 969139, artist: 354493, name: "Champ", gid: "15a9a4b8-9dd9-4f6f-b4e9-7c69948af88f", modpending: 0, attributes: 1100, page: 143735328, language: 120, script: 28, modpending_lang: nil, quality: -1, modpending_qual: 0> 
ruby-1.9.2-p0 > x.name
ActiveRecord::DangerousAttributeError: attributes_before_type_cast is defined by ActiveRecord
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:23:in `instance_method_already_implemented?'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:263:in `block (2 levels) in define_attribute_methods'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `each'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `block in define_attribute_methods'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `each'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `define_attribute_methods'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:13:in `define_attribute_methods'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:41:in `method_missing'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/thwart-0.0.4/lib/thwart/canable.rb:27:in `method_missing'
  from (irb):2
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
  from /Users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'
ruby-1.9.2-p0 > 

这看起来好像属性的名称被保留,所以我想找到一些办法忽略它的所有查询,并在反射时有AR忽略它模式来定义的模型类。有什么建议么?谢谢!

It looks as if the attributes name is reserved, so I'd like to find some way to ignore it for all queries and have AR ignore it when reflecting on the schema to define the model class. Any suggestions? Thanks!

推荐答案

解决这个罗宾的链接和一些其他的SO答案用的东西相结合

Solved this using a combination of stuff from Robin's link and some other SO answers

class Album < ActiveRecord::Base
  set_table_name "album"

  class << self
    def instance_method_already_implemented?(method_name)
      return true if method_name =~ /^attributes/
      super
    end
  end

  belongs_to :artist
  has_many :tracks, :through => :album_tracks
end

的伎俩。我用了一个大的巨大变化返回true而不发出一个错误的所有方法开始属性,我不认为它造成其他地方的任何问题。

Did the trick. I used a big sweeping change to return true without throwing an error for all methods starting with attributes, and I don't think its caused any problems elsewhere.

这篇关于我怎样才能永久地忽略我的ActiveRecord :: Base类的数据库列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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