[Nitro] Og newbie problems
Aleksi Niemela
Aleksi.Niemela at cs.helsinki.fi
Thu Aug 11 12:57:45 EDT 2005
Kristof Jozsa wrote:
> Hi,
>
Hello there, nice to see you got yourself going well fast!
> The first problem I bumped into can be demonstrated with the following
> piece of code:
> class LineItem
> belongs_to Order
> 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?
>
There's patch for this functionality already. But I'm not sure if George
ended up adding a new keyword just to enchant the class with relation
macros. Refer to
http://rubyforge.org/pipermail/nitro-general/2005-June/000448.html
if you want to implement it by yourself.
> 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?
>
Sure. Here's the patch. I wasn't able to test it but doesn't seem
complicated enough to really break anything, and I'm sure George will
test it before commiting.
$ svn diff psql.rb
Index: psql.rb
===================================================================
--- psql.rb (revision 250)
+++ psql.rb (working copy)
@@ -218,6 +218,7 @@
begin
create_join_table_sql(info).each do |sql|
@conn.exec(sql).clear
+ Logger.info "Created jointable '#{info[:table]}'."
end
rescue Object => ex
# gmosx: any idea how to better test this?
> 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:
>
The example at the end of this mail works for me. So perhaps you can
adapt to similar technique until George has sorted this out at some
later revision.
- Aleksi
#!/usr/bin/ruby -rubygems
$DBG = true
require 'test/unit'
require 'og'
#require 'og/relation'
class Order
property :name, String
has_many :items, LineItem
def initialize(name, its)
@name = name
save!
its.each { |it| items << it }
end
end
class LineItem
property :name, String
belongs_to Order
def initialize(name)
@name = name
end
end
class Software
property :name, String
def initialize(name)
@name = name
end
end
class SoftwarePackage < Software
property :name, String
many_to_many :software, Software
def initialize(name, softwares)
@name = name
save!
softwares.each { |x| add_software x }
end
end
class TestOg < Test::Unit::TestCase
include Og
def test_basic
items = [LineItem.create("foo"),
LineItem.create("bar")]
o = Order.create("order", items)
sws = [Software.create("og"),
Software.create("nitro")]
o = SoftwarePackage.create("package", sws)
end
def setup
db_conf = {
:name => "testing2",
:store => "psql"
}
og = Og.setup(db_conf)
end
end
More information about the Nitro-general
mailing list