私はレールの外でActive Recordを使用しています。私は以下のような2つのARクラスを持っています:
class Building < ActiveRecord::Base
has_many :rooms
serialize :current_room, Room
そして
class Room < ActiveRecord::Base
belongs_to :building
I had originally migrated the database tables to have the current_room column be declared as a :room
instead of a :binary
, which worked, but made my db schema unusable due to the unknown type, そして potentially would make my implementation non database independent. (using sqlite3, haven't tried another yet)
:room
からテーブルの列タイプを:binary
に変更したとき、建物をナビゲートするときにcurrent_room変数を変更しようとすると「未定義メソッド」エラーが発生しました部屋から部屋まで。このエラーは、dがビルディングオブジェクトであるirbでは次のようになります。
irb(main):006:0> d.current_room = d.rooms.first
NoMethodError: undefined method `gsub' for #
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/attribute_methods.rb:385:in `method_missing'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/attribute_methods.rb:60:in `method_missing'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/sqlite_adapter.rb:24:in `binary_to_string'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/column.rb:84:in `type_cast'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/attribute_methods/dirty.rb:89:in `field_changed?'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/attribute_methods/dirty.rb:63:in `write_attribute'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/attribute_methods/write.rb:14:in `current_room='
from (irb):6
from C:/Ruby192/bin/irb:12:in `'
I presume it is because the object attribute isn't treating it like a Room object, which is understそしてable, but I thought the serialize method solved that issue. d.current_room.class
そして d.rooms.first.class
both return Room as the object class, as well. Is there a way to have this work properly without overwriting the assignment methods?