weixin_39782500 2020-11-22 01:58
浏览 0

STI + Text on child + each bug

Hey again

Having more STI problems. This time, it's accessing a text property only on a child object.

This is as far down as I can get the bug: http://pastie.org/517114

The error only happens when text is defined on the subclass, and I access the text value through a separate method inside of an each call.

Gem versions are 0.9.11 - let me know if there's any more info needed.

Thanks!

david

Created by David Kelso - 2009-06-18 23:58:48 UTC

Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/911

该提问来源于开源项目:datamapper/dm-core

  • 写回答

10条回答 默认 最新

  • weixin_39782500 2020-11-22 01:58
    关注

    Hmm, while the :content property is definitely stored inside the elements table because that’s how STI does its thing, I do think that it’s correct that the Element model (baseclass) does not understand it since it’s actually declared in Textbug model (subclass). At least that’s how I understand inheritance.

    Also, the error message explicitly states that there is no such property :content in the Element model, which is true, it’s only in the Textbug model

    I’m marking this as not applicable.

    For reference, I stripped your script to the minimum (basically I removed the indirection through the additional method since it’s not necessary to show the behavior) and ran it with latest next branch. The error message is better than the one you got with 0.9.11 and imho it’s how it should behave.

     ruby
    require ’rubygems’
    require ’dm-core’
    require ’dm-validations’
    
    class Element
      include DataMapper::Resource
    
      property :id, Serial
      property :type, Discriminator
    end
    
    class Textbug < Element
      property :content, Text
      validates_present :content
    end
    
    DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/content.db")
    DataMapper.auto_upgrade!
    
    box = Textbug.create({:content => ’hello’})
    Element.all.each do |e|
      puts e.content
    end
    
    # mungo:snippets snusnu$ ruby 911.rb 
    # /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:741:in `assert_valid_fields’: +options[:field]+ entry :content does not map to a property in Element (ArgumentError)
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:732:in `each’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:732:in `assert_valid_fields’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:708:in `assert_valid_options’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:706:in `each’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:706:in `assert_valid_options’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:648:in `initialize’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:354:in `update’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/query.rb:370:in `merge’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/collection.rb:73:in `reload’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:817:in `reload_attributes’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:802:in `lazy_load’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/property.rb:660:in `send’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/property.rb:660:in `lazy_load’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/property.rb:539:in `get’
    #   from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/model/property.rb:206:in `content’
    #   from 911.rb:22
    #   from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:458:in `each’
    #   from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:458:in `each’
    #   from 911.rb:21
    

    by Martin Gamsjaeger (snusnu)

    评论

报告相同问题?