ruby on rails - How can I permanently ignore a database column in my ActiveRecord::Base class? -
i have legacy database i'm trying model using rails. 1 of tables has column named attributes, name reserved rails think.
here sql table:
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', ... ); here's activerecord class:
class album < activerecord::base set_table_name "album" belongs_to :artist has_many :tracks, :through => :album_tracks end here's happens when try instantiate instance:
hornairs@bishop:~/sites/logdb (master *)$ rails c loading development environment (rails 3.0.3) no such file 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 defined activerecord /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?' /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' /users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:262:in `each' /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' /users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:261:in `each' /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' /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' /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' /users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/thwart-0.0.4/lib/thwart/canable.rb:27:in `method_missing' (irb):2 /users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start' /users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start' /users/hornairs/.rvm/gems/ruby-1.9.2-p0@logdb/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>' script/rails:6:in `require' script/rails:6:in `<main>' ruby-1.9.2-p0 > it looks if attributes name reserved, i'd find way ignore queries , have ar ignore when reflecting on schema define model class. suggestions? thanks!
solved using combination of stuff robin's link , other 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 did trick. used big sweeping change return true without throwing error methods starting attributes, , don't think caused problems elsewhere.
Comments
Post a Comment