#155 ✓resolved
coxy

No type constraint added when querying subclasses in STI

Reported by coxy | February 13th, 2008 @ 03:49 AM

class Article < DataMapper::Base 
    property :title, :string 
end

class SubArticle < Article 
    property :subtitle, :string 
end 
--- 
Article.create :title => "An Article" 
SubArticle.create :title => "A SubArticle", :subtitle => "A SubArticle SubTitle" 
-- 
Article.count 
=> 2 # seems correct 
SubArticle.count 
=> 2 # shouldn't there only be 1? 
--- 

Comments and changes to this ticket

  • Michael Boutros

    Michael Boutros February 14th, 2008 @ 07:26 AM

    I wrote a fix for this that I believe will fix it for good, but last night I was having a hard with git (my fault, not git's). I'll try to have the patch up here tonight.

  • coxy

    coxy February 15th, 2008 @ 06:19 AM

    Excellent....thanks, I look forward to seeing it.

  • Nick Plante

    Nick Plante February 15th, 2008 @ 04:58 PM

    • Milestone cleared.
    • State changed from “new” to “open”
  • Michael Boutros

    Michael Boutros February 18th, 2008 @ 07:02 PM

    I promise I'll get the patch up here ASAP. I finally figured git out but then my aunt had a baby and I had a trip and now I have a huge test tomorrow, but tomorrow night I'll be free at last!

  • Michael Boutros

    Michael Boutros February 19th, 2008 @ 03:35 PM

    Attached is the patch plus an rSpec test for it.

  • coxy

    coxy February 21st, 2008 @ 03:24 AM

    That's great thanks - it works great! I had a 'subclass?' undefined error to start with but adding this to DataMapper::Persistence::ClassMethods seems to have done the trick:

          def subclass?
            superclass != DataMapper::Base
          end
    
  • Michael Boutros

    Michael Boutros February 21st, 2008 @ 03:31 PM

    Woops, the new patch should work fine. I didn't change the #subclass? method to your way, however, because if I did then models which included Persistence directly wouldn't work.

  • coxy

    coxy February 22nd, 2008 @ 02:48 AM

    Good point! Thanks again.

  • Kevin Bullock

    Kevin Bullock February 27th, 2008 @ 04:52 PM

    The patch doesn't actually work; in the inherited method defined on each class in DM::Persistence.included, you need to define self.subclass? instead of just subclass?.

    Updated patch (fix_sti_2.diff) attached.

  • Michael Boutros

    Michael Boutros April 23rd, 2008 @ 09:23 PM

    Did this ever get resolved?

  • Sam Smoot

    Sam Smoot May 2nd, 2008 @ 04:49 AM

    • Assigned user set to “Adam French”

    No. We'll apply the patch to 0.3.2 if it still does.

    0.9.0 will provide a :scope option for properties, so strictly speaking, this will be resolved by that, not any STI specific code. DM 0.9.0 probably won't actually have STI specific code. Just an inheritance hook to copy properties (already in place), and a property like this:

    property :type, Class,
      :default => lambda { |resource, property| resource.class.name },
      :scope => lambda => { |model| { :type => model } }
    

    In the mean-time, Adam, please apply this against DM-SVN.

  • Adam French

    Adam French May 2nd, 2008 @ 03:12 PM

    patch applied to 0.3.x r748. Someone please run the specs, my local box is borked

  • Adam French

    Adam French May 2nd, 2008 @ 03:12 PM

    • State changed from “open” to “resolved”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Attachments

Tags

Pages