Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Expected behaviour of polymorphic has_many relationship
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ashley Moran  
View profile  
 More options Nov 5 2006, 12:46 pm
From: Ashley Moran <w...@ashleymoran.me.uk>
Date: Sun, 5 Nov 2006 17:46:00 +0000
Subject: Expected behaviour of polymorphic has_many relationship
Hi

I posted something similar to this on the Rails-takl list but I've  
just discovered the core list and Iguess this is a better place to  
ask.  I've been doing a bit of work on inheritance in ActiveRecord  
and my changes broke this test from associations/join_model_test.rb:

   def  
test_polymorphic_has_many_create_model_with_inheritance_and_custom_base_
class
     post = SubStiPost.create :title => 'SubStiPost', :body =>  
'SubStiPost body'
     assert_instance_of SubStiPost, post

     tagging = tags(:misc).taggings.create(:taggable => post)
     assert_equal "SubStiPost", tagging.taggable_type
   end

I was getting '<"SubStiPost"> expected but was <"Post">'.  To fix  
this I modified a line in this section of the belongs_to method in  
associations.rb:

   if reflection.options[:polymorphic]
     association_accessor_methods(reflection,  
BelongsToPolymorphicAssociation)

     module_eval do
       before_save <<-EOF
         association = instance_variable_get("@#{reflection.name}")
         if association && association.target
           if association.new_record?
             association.save(true)
           end

           if association.updated?
             self["#{reflection.primary_key_name}"] = association.id
   -         self["#{reflection.options[:foreign_type]}"] =  
association.class.base_class.name.to_s
   +         self["#{reflection.options[:foreign_type]}"] =  
association.class.name.to_s
           end
         end
       EOF
     end
   else
     ...
   end

But that change breaks these tests:

   def test_polymorphic_has_many_create_model_with_inheritance
     post = posts(:thinking)
     assert_instance_of SpecialPost, post

     tagging = tags(:misc).taggings.create(:taggable => post)
     assert_equal "Post", tagging.taggable_type
   end

   def test_polymorphic_has_one_create_model_with_inheritance
     tagging = tags(:misc).create_tagging(:taggable => posts(:thinking))
     assert_equal "Post", tagging.taggable_type
   end

Both now fail on <"Post"> expected but was <"SpecialPost">.

What I don't understand is that the fixture Post :thinking is  
actually a SpecialPost, so why is that not the correct taggable_type  
value?  I can't figure out what the intended behaviour of this  
association.

I'd be really grateful for an explanation of this, it's the only  
thing I can't make work.

Ashley


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
evn  
View profile  
 More options Nov 20 2006, 4:41 pm
From: "evn" <ewea...@gmail.com>
Date: Mon, 20 Nov 2006 21:41:23 -0000
Local: Mon, Nov 20 2006 4:41 pm
Subject: Re: Expected behaviour of polymorphic has_many relationship

What are your original changes, that broke the tests initially?

Evan

On Nov 5, 12:46 pm, Ashley Moran <w...@ashleymoran.me.uk> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ashley Moran  
View profile  
 More options Nov 24 2006, 4:45 am
From: Ashley Moran <w...@ashleymoran.me.uk>
Date: Fri, 24 Nov 2006 09:45:41 +0000
Local: Fri, Nov 24 2006 4:45 am
Subject: Re: [Rails-core] Re: Expected behaviour of polymorphic has_many relationship

On 20 Nov 2006, at 21:41, evn wrote:

> What are your original changes, that broke the tests initially?

> Evan

Hi Evan

It was my attempt at class-table inheritance here: http://
dev.rubyonrails.org/ticket/6566

It's not finished yet, because I haven't made it work with  
associations, but the ONLY thing it breaks in the existing tests (ie  
if you use a CTI-enabled ActiveRecord but don't actually use CTI) is  
polymorphic has_many relationships.  I think this is because I've  
redefined the meaning of "abstract class".

Any ideas?

Ashley


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google