ruby-on-rails – 如何永久忽略ActiveRecord :: Base类中的数据库列?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何永久忽略ActiveRecord :: Base类中的数据库列?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个遗留数据库,我正在尝试使用Rails进行建模.其中一个表有一个名为attributes的列,我认为这是Rails保留的名称.

这是表的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类:

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

以下是我尝试实例化实例时发生的情况:

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忽略它.有什么建议?谢谢!

解决方法

使用罗宾链接和其他一些SO答案的组合解决了这个问题
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而不会为所有以属性开头的方法抛出错误,我不认为它在其他地方引起任何问题.

猜你在找的Ruby相关文章