gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:49:53 25 4
gpt4 key购买 nike

我有一个旧数据库,我正在尝试使用 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 >

看起来好像 attributes 名称是保留的,所以我想找到一些方法来忽略所有查询的它,并让 AR 在反射(reflect)架构以定义模型类时忽略它。有什么建议么?谢谢!

最佳答案

结合使用 Robin 链接中的内容和其他一些 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,而不会对以 attributes 开头的所有方法抛出错误,而且我认为它不会在其他地方引起任何问题。

关于ruby-on-rails - 如何永久忽略 ActiveRecord::Base 类中的数据库列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4911174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com