[Nitro] many_to_many delete bug

Robert Mela rob at robmela.com
Thu Nov 15 10:07:43 EST 2007


While you're in there there's an oddity in Collection#push.

  post.tags << tag1 # '<<' is alias for 'push'
  post.tags << tag2


Collection has an array of objects called 'members'.  With each object 
addition, Collection#push calls members.include? to see if the object is 
already in the array.  If so, it removes the object before re-adding.

The odd thing is that, even though tag2 had not been previously added, 
members.include? returns true.   This traces back to the fact that

   t1 === t2

returns true if the tags have not yet been saved to the database, and 
returns false if they have been saved and reloaded.  So it's probably 
using the database ID.

Fortunately for my workaround (members.delete) the internal search in 
Array#delete unsaved Tag objects doesn't use the same comparison!




George Moschovitis wrote:
> I will fix this (it is a showstopper for my current application too!!)
>
> -g.
>
> On Nov 14, 2007 3:59 PM, Robert Mela <rob at robmela.com 
> <mailto:rob at robmela.com>> wrote:
>
>     Just for reference -- the other showstopper is in
>     http://rubyforge.org/pipermail/nitro-general/2007-November/008957.html
>
>     The new issue is delete in a many_to_many relation:
>
>     robert-melas-computer:~/projects/og rmela$ ruby tagtest.rb  INFO: Og
>     uses the Sqlite store.
>     Program inserting tag1
>     Collection#initialize: Collection initialized with remove_proc =
>     remove_tag
>     Created tags collection with remove proc=remove_tags
>     Collection#push Entered Collection.push(tag1) with tag=tag1.
>      Owner is
>     #<Post:0x15911e4>
>     Program calling save
>     Deleting tag
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/store/sql.rb:144:in
>     `og_delete': uninitialized constant
>     Og::SqlEnchantmentMixin::BelongsTo
>     (NameError)
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/store/sql.rb:139:in
>     `each'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/store/sql.rb:139:in
>     `og_delete'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/store.rb:216:in
>     `transaction_raise'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/model.rb:196:in
>     `transaction_raise'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/manager.rb:118:in
>     `with_store'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/model.rb:195:in
>     `transaction_raise'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/store/sql.rb:137:in
>     `og_delete'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/store.rb:153:in
>     `delete'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/model.rb:181:in
>     `delete'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/manager.rb:118:in
>     `with_store'
>            from
>     /Users/rmela/nitro/branch/script/lib/../../og/lib/og/model.rb:180:in
>     `delete'
>            from tagtest.rb:31
>
>
>     Here's code that triggers it.   I tried p.remove_tag(t1) and p.save!
>     before the delete -- still no dice.   Even if that did work it'd be a
>     workaround, and unintuitive.
>
>     #!/opt/local/bin/ruby
>     require 'og'
>     require 'sqlite3'
>
>     class Post
>     attr_accessor :name, String
>     is Taggable
>     end
>
>     Og.start(
>     :adapter => 'sqlite',
>     :name => 'tagtest',
>     :destroy_schema => true,
>     :create_schema => true
>     )
>     p=Post.new
>     p.name <http://p.name> = 'post1'
>     t1 = Tag.new
>     t1.name <http://t1.name> = 'tag1'
>
>     puts "Program inserting tag1"
>     p.tags <<  t1
>     puts "Program calling save"
>     p.save!
>     puts "Deleting tag"
>     t1.delete
>
>
>
>
>     _______________________________________________
>     Nitro-general mailing list
>     Nitro-general at rubyforge.org <mailto:Nitro-general at rubyforge.org>
>     http://rubyforge.org/mailman/listinfo/nitro-general
>     <http://rubyforge.org/mailman/listinfo/nitro-general>
>
>
>
>
> -- 
> http://me.gr
> http://joy.gr
> http://cull.gr
> http://nitroproject.org
> http://phidz.com
> http://joyerz.com 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: rob.vcf
Type: text/x-vcard
Size: 116 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/nitro-general/attachments/20071115/69a9b565/attachment.vcf 


More information about the Nitro-general mailing list