我有一個遺留數據庫,我試圖使用Rails進行建模。其中一個表有一個名爲attributes
的列,我認爲這是一個由Rails保留的名稱。如何永久地忽略ActiveRecord :: Base類中的數據庫列?
下面是表的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
這裏是當我嘗試實例化一個實例會發生什麼:
[email protected]:~/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/[email protected]/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:23:in `instance_method_already_implemented?'
from /Users/hornairs/.rvm/gems/[email protected]/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/[email protected]/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `each'
from /Users/hornairs/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `block in define_attribute_methods'
from /Users/hornairs/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `each'
from /Users/hornairs/.rvm/gems/[email protected]/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `define_attribute_methods'
from /Users/hornairs/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:13:in `define_attribute_methods'
from /Users/hornairs/.rvm/gems/[email protected]/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:41:in `method_missing'
from /Users/hornairs/.rvm/gems/[email protected]/gems/thwart-0.0.4/lib/thwart/canable.rb:27:in `method_missing'
from (irb):2
from /Users/hornairs/.rvm/gems/[email protected]/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /Users/hornairs/.rvm/gems/[email protected]/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /Users/hornairs/.rvm/gems/[email protected]/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 >
它看起來好像attributes
名稱是保留的,所以我想找到一些方法來忽略它查詢並在反映模式以定義模型類時讓AR忽略它。有什麼建議麼?謝謝!
我不知道解決方案,但我發現與某人有相同問題的線程:。不知道它會有幫助。 –
Robin
2011-02-06 02:48:36