faol yozuvlar sinfi obyektning qiymatini e'lon qilingan ikkilik atributga tayinlashda 'method_missing' ni belgilaydi

Men rezyumendan tashqari Active Record dan foydalanmoqdaman va menda quyidagi ikkita AR sinf mavjud:

class Building < ActiveRecord::Base
   has_many :rooms
   serialize :current_room, Room

va

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, va potentially would make my implementation non database independent. (using sqlite3, haven't tried another yet)

Jadval ustunini turini : room dan : ikkilik ga o'zgartirganimda, binodan harakat qilayotib, current_room o'zgaruvchisini o'zgartirishga urinayotganda "undefined method" xatoini olishga muvaffaq bo'ldim xonaga xona. Xato shu kabi ko'rinadi, bu erda d - bino ob'ekti:

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 understvaable, but I thought the serialize method solved that issue. d.current_room.class va 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?

0
Faqat FYI - bu sizning savolingizga javob bo'lsa, unda siz bu muammoni hal qiluvchi keyingi shaxs uchun javob sifatida yozishingiz mumkin (va da'vat qilinadi) va sizning savolingizni topadi. 24 soatdan keyin o'zingizning javobingizni qabul qilishingiz mumkin.
qo'shib qo'ydi muallif jefflunt, manba
Menga bu ketma-ketlikning matnli ustunlar uchun yozilganidan keyin darhol kelib qoldi va men uni sinab ko'rdim, u ishladi. Shunga qaramay, qarama-sezgir ko'rinadi. Agar men faqatgina matndagi narsalarni seriyalashtirishim mumkin bo'lsa, nima uchun men ikkilamchi foydalanishi mumkin?
qo'shib qo'ydi muallif BitPuncher, manba

2 javoblar

Sizning xato xabaringiz xususida aniq javob beraman. Men odatda xato xabarlarini diqqat bilan o'qib chiqsam, odatda noto'g'ri nima ekanligini aytib beraman. Demak, bu holatda ...

NoMethodError: undefined method `gsub' for #

It's saying that gsub is not defined for Room. So basically, your code at some point is trying to do something like...

room_instance.gsub('something', 'else')

So I don't think it's a problem with binary vs string. I'd look at your code and search for gsub to see exactly where the problem is. Also, gsub is usually called on strings.

1
qo'shib qo'ydi
Ha, bu muammo, ammo menimcha, bu narsa noto'g'ri yo'l bilan biror narsa qilishga urinish natijasida kelib chiqadigan begona edi. Kodni tekshirdim va gsubni o'zimning hech bir joyda chaqirmayman. Ehtimol, xato kodining uchinchi qatorida joylashgan binary_to_string da chaqiriladi.
qo'shib qo'ydi muallif BitPuncher, manba

Serileştirme, ikki tomonlama emas, mag'lubiyatga ustun turlari uchun mo'ljallangan. Ustun turi mag'lubiyatga o'zgartirilganda, hamma narsa to'g'ri ishlaydi.

0
qo'shib qo'ydi