[Nitro] HasManyCollection#each

Aleksi Niemela Aleksi.Niemela at cs.helsinki.fi
Mon Nov 28 12:58:17 EST 2005


itsme213 wrote:

>I tried #<< to add objects to the relation.
>
>#each still does not work for me.
>
>Thanks.
>
>-----------------
>require 'og'
>...
>
>puts b.posts.count #=> 0
>  
>
>b.posts.each {|a| puts a.name} #=> nothing
>  
>
Here's some code that show you few key points leading to different 
behavior. Be sure to check out the contents of the DB afterwards. I had 
some point having 4 different cases in the code, but I forgot already 
what was so different between version 1 and 3. Maybe the result wasn't 
same from the DB point of view. Maybe something else. The code and the 
output I get from the code is at the end of the mail.

> I hate to be asking such basic questions, but not having a working rdoc 
> (nitro 0.25) makes it quite hard to get started.

Oh, and that rdoc thing. Dunno why George's advice didn't work for you, 
but this is what I do and works for me:

   cd og-0.25.0
   rdoc -o rdoc

    - Aleksi


require 'rubygems'
require 'og'

class A
 property :name, String
 belongs_to A
 def initialize(n)
  @name = n
 end
end

class B
 property :name, String
 has_many :posts, A
end

Og.setup(
        :address => "localhost",
        :destroy => false,
        :name => "testing",
        :store => "psql",
        :user => "...",
        :password => "....",
        :connection_count => 1
)

4.times do |i|
  puts "Version #{i}"

  b = B.new
  b.name = "#{i}_foo"

  if i == 0 or i == 1
    puts "Making new objects don't save them immediately"
    a1 = A.new "#{i}_a1"
    a2 = A.new "#{i}_a2"
  else
    puts "Calling create will instantiate like new also save them to the db"
    a1 = A.create "#{i}_a1"
    a2 = A.create "#{i}_a2"
  end
 
  if i == 0 or i == 2
    puts "Saving b before adding posts, ie. b.oid has value and A 
objects will get value for b_oid"
    b.save
  end
 
  puts "b.oid = #{b.oid.inspect}"
  b.posts << a1
  puts "a1.b_oid = #{a1.b_oid.inspect}"

  b.posts << a2

  if i == 1 or i == 3
    puts "By saving b after addings posts it will get oid, but a1.b_oid 
and a2.b_oid have"
    puts "already been set to original b.oid which has been nil."
    puts "In these cases object b.posts collection will know added 
objects a1 and a2 but"
    puts "if it's fetched again from database the collection won't know"
    puts "(as a1 and a2 objects in database have nils as a1.b_oid and 
a2.b_oid, thus they don't"
    puts "belong to any B object."
    b.save
  end
 
  puts "b.posts collection has #{b.posts.count} objects"
  b.posts.each {|a| puts a.name} #=> nothing

  b2 = B.find_by_name("#{i}_foo")
  puts "Freshly fetched b has #{b2.posts.count} objects associated to it 
in the db"
  b2.posts.each {|a| puts a.name} #=> nothing

  puts "\n\n"
end


###########
###########

$ ruby hasmanycollection_each.rb
I, [2005-11-28T19:54:55.101125 #4040]  INFO -- : Created table 'ogb'.
I, [2005-11-28T19:54:55.301125 #4040]  INFO -- : Created table 'oga'.
Version 0
Making new objects don't save them immediately
Saving b before adding posts, ie. b.oid has value and A objects will get 
value for b_oid
b.oid = 1
a1.b_oid = 1
b.posts collection has 2 objects
0_a1
0_a2
Freshly fetched b has 2 objects associated to it in the db
0_a1
0_a2


Version 1
Making new objects don't save them immediately
b.oid = nil
a1.b_oid = nil
By saving b after addings posts it will get oid, but a1.b_oid and 
a2.b_oid have
already been set to original b.oid which has been nil.
In these cases object b.posts collection will know added objects a1 and 
a2 but
if it's fetched again from database the collection won't know
(as a1 and a2 objects in database have nils as a1.b_oid and a2.b_oid, 
thus they don't
belong to any B object.
b.posts collection has 0 objects
Freshly fetched b has 0 objects associated to it in the db


Version 2
Calling create will instantiate like new also save them to the db
Saving b before adding posts, ie. b.oid has value and A objects will get 
value for b_oid
b.oid = 3
a1.b_oid = 3
b.posts collection has 2 objects
2_a1
2_a2
Freshly fetched b has 2 objects associated to it in the db
2_a1
2_a2


Version 3
Calling create will instantiate like new also save them to the db
b.oid = nil
a1.b_oid = nil
By saving b after addings posts it will get oid, but a1.b_oid and 
a2.b_oid have
already been set to original b.oid which has been nil.
In these cases object b.posts collection will know added objects a1 and 
a2 but
if it's fetched again from database the collection won't know
(as a1 and a2 objects in database have nils as a1.b_oid and a2.b_oid, 
thus they don't
belong to any B object.
b.posts collection has 0 objects
Freshly fetched b has 0 objects associated to it in the db





More information about the Nitro-general mailing list