[Nitro] og: automatic deletion of records in M:N relations?

George Moschovitis george.moschovitis at gmail.com
Wed Aug 23 07:51:06 EDT 2006


I will test this later today, and let you know tommorow.

regards,
George.

On 8/22/06, Stephan Mueller <d454d at web.de> wrote:
> Hi,
>
> I tried now the suggested solution but have to say, it does not work as
> intended.
>
> > > > Try to set the relation only to other of the classes (it's two-way
> > > > automatically).
> > > >
> > > > Also, try this:
> > > >
> > > > class Item
> > > >     # ...
> > > >     joins_many Tag
> > > >     # ...
> > > > end
> > > >
> > > > class Tag
> > > >     # ...
> > > >     belongs_to Item
> > > >     # ...
> > > > end
> > > >
> > > > so Tags get deleted if the Item is deleted, I don't know why you would
> > > > want to remove the Items if a Tag is removed...
>
> This does not work for me because i need to have the tags independent
> from the items. An item can have a list of different tags and a tag can
> belong to several items.
>
> So i tried to set the relation only in one class, code follows:
>
> cat m_n.rb
>
> #!/usr/bin/ruby
> #
>
> require 'rubygems'
> require 'og'
>
> class Item
>   property :name, String
>
>   joins_many Tag
>
>   def to_s
>     @name
>   end
> end
>
> class Tag
>   property :name, String
>
>   def to_s
>     @name
>   end
> end
>
> Og.setup(:store => :sqlite, :name => 'm_n')
>
> (1..3).each { | n |
>   t = Tag.new
>   t.name = "Tag_" + n.to_s
>   t.save
>   puts "created tag with oid " + t.oid.to_s
> }
>
> # see if i can get a tag back from th db
> t = Tag.find_by_name("Tag_1")
> t.nil? ? puts("no tag found") : puts("found tag: " + t.name)
>
> i1 = Item.new
> i1.name = "Item_1"
> i1.save
> i1.add_tag(Tag.find_by_name("Tag_1"))
> i1.add_tag(Tag.find_by_name("Tag_2"))
> i1.save
>
> i2 = Item.new
> i2.name = "Item_2"
> i2.save
> i2.add_tag(Tag.find_by_name("Tag_2"))
> i2.add_tag(Tag.find_by_name("Tag_3"))
> i2.save
>
> # part 2
> puts "please inspect db now :)\n"
> gets
> i1.delete
> i2.delete
> puts "and again...\n"
>
>
>
> Here are my results from the db:
>
> First part:
>
> sqlite> select * from ogitem;
> Item_1|1
> Item_2|2
> sqlite> select * from ogtag;
> Tag_1|1
> Tag_2|2
> Tag_3|3
> sqlite> select * from ogj_item_tag;
> 1|1
> 1|2
> 2|2
> 2|3
>
> Second part (after deleting i1 and i2)
>
> sqlite> select * from ogj_item_tag;
> 1|1
> 1|2
> 2|2
> 2|3
> sqlite> select * from ogtag;
> Tag_1|1
> Tag_2|2
> Tag_3|3
> sqlite> select * from ogitem;
>
> ogj_item_tag is still not empty... ;)
>
> Any ideas?
>
>
> Cheers,
>
> Steph.
> _______________________________________________
> Nitro-general mailing list
> Nitro-general at rubyforge.org
> http://rubyforge.org/mailman/listinfo/nitro-general
>


-- 
http://www.gmosx.com
http://www.nitroproject.org


More information about the Nitro-general mailing list