[Nitro] Og newbie problems
Kristof Jozsa
dyn at ond.vein.hu
Thu Aug 11 11:10:50 EDT 2005
Hi,
admittedly I started to play with Og today so please feel free to correct me
with anything (or just send over /dev/null :), but I'd like to share my
experiences and ask for suggestions. All my experiments happened on my gentoo
linux box, Og installed by gem, version 0.22.0 on ruby 1.8.2.
The first problem I bumped into can be demonstrated with the following piece of
code:
----
#!/usr/bin/ruby -rubygems
require 'test/unit'
require 'og'
require 'og/relation'
class Order
property :name, String
has_many :items, LineItem
end
class LineItem
belongs_to Order
end
class TestOg < Test::Unit::TestCase
include Og
def test_basic
end
def setup
og = Og.setup(:store => :memory, :name => 'test')
end
end
----
running this script dumps this error:
/test_og.rb:15: undefined method `belongs_to' for LineItem:Class (NoMethodError)
I kept on fighting and after some time I realized that adding any property to
LineItem before the belongs_to declaration fixes the problem. Is there any other
way to trigger initializing Og (I think that's what fails to happen in this
case) than this?
Next I had set up an unidirectional many-to-many relationship. I tried very hard
to set it up correctly and get the join table generated, right until I realized
that it IS generated just not in the logs.. (the two other create table sql
statements were displayed as INFO messages but not the join table :) Do you
think it would make sense displaying that info as well?
Still at the many-to-many relationship (example was many SoftwarePackage can
contain many Software), I tried to create a constructor for SoftwarePackage
which accepted an array of Software (simplifying the example here):
----
class Software
property :name, String
end
class SoftwarePackage < Software
many_to_many :software, Software
def initialize(software)
software.each { |x| add_software x }
end
end
----
and well, it did not really work out. I find out that I have to call save first
in the SoftwarePackage constructor, else an invalid sql will be generated like:
INSERT INTO j_software_softwarepackage (software_oid,softwarepackage_oid) VALUES
(1, )
with a missing id. Again, I've got a workaround here so I'm not sure if that's
the way it's meant to work or that's clarified as a rough edge and you plan to
smooth it a bit :) Still at this topic slightly against following the least
surprise principle first I assumed the following line to work in the constructor:
software.each { |x| @software << x }
Maybe it would be nice to have that too if possible.
As a last question, is there a way to change the oid naming strategy followed by
Og? A similar approach as setting Og.table_prefix = nil would be really useful.
Wrapping up, I'm absolutely amazed with the power of Og. I'm actually sketching
a new java project's software design from scratch with ruby and Og (first time
attempting this), and I'm blown away with the speed I could build/test and play
around with my domain objects using Og.
Thanks much, and keep up the excellent work,
Kristof Jozsa
More information about the Nitro-general
mailing list