Gwazy asked:

Working with Og, But im a little lost...

Ok so I am making a forum as my learning project into nitro, now I have only been using ruby for a few weeks now and I find it simple and easy coming from c++. I Have the following dbmodel:

class User
 property :login, String
 property :password, String
 property :first_name, String
 property :last_name, String
 property :email, String
 property :date_created, Date
 property :user_id, Integer
end

class Forum
 property :forum_posts, Integer
 property :amount_of_forum_posts, Integer
 property :name_of_forum, String
end

class Reply
 property :date_replied, Time
 property :reply_message, String
 property :reply_owner, String
 property :replied_too, String
end

class Post
 property :post_created_by, String
 property :post_created_date, Time
 property :post_title, String
 property :post_message, String
end



Now I just have a few general questions and need some pointers.

Q: How do I pull && add && search through data via Og using sqlite?

Q: Is my current db model good? What could I do to fix it and optomize it?

Q: Is there a way to make sure that my database code can be used on any dbserver the user wants? I.E. psql, mySQL, oracle?

Q: How do I use forms to interact with said data? Do I make a .xhtml in public with the same name as the defined page? And If I do that will they still work in sync?

Q: After looking through the Rdoc of Og I saw HasOne and Hasmany and other relationship classes, Should I use these? And How should I use them?

I know thats allot of questions but I only need to know a few things and I can basiclly take care of myself :)

Also any suggestions/ideas are welcome!

(1 attempts)

Kashia answered:

[UPDATE 2006-11-15]

Set to answered due to unresponsiveness of the questioner.

Q: How do I pull && add && search through data via Og using sqlite?

Through Sqlite

Og.setup(:store => 'sqlite', :name => 'database_name')

Add

This will add a new Post item.

a = Post.new
a.title = 'test title'
# ...
a.save

Pull

This will pull a Post with a known 'oid', which is just a field in the post table and is the standard primary key.

a = Post[5]

Search

a = Post.find(:condition => "title = 'test title'")
a = Post.find(:where => "title = 'test title'")
a = Post.find(:sql => "select * from ogpost where title = 'test title'")
# etc....

There are many more styles to search in the database, look out for EZ, if you are the non SQL type, otherwise the :sql is the most flexible.

Q: Is my current db model good? What could I do to fix it and optomize it?

This is probably how I would write this, using is Timestamped and relations (like has_many) simplifies the matters quite a bit.

require 'glue/timestamped'

class User
  is Timestamped
 
  property :login, String
  property :password, String
  property :first_name, String
  property :last_name, String
  property :email, String

  has_many :posts, Post
end

class Forum
  is Timestamped
  property :name, String
  has_many :posts, Post
end

class Reply
  is Timestamped
  property :message, String

  belongs_to :owner, User
  has_one :replied_to, Post
  

  def date_replied
    return @create_time
  end
end

class Post
  is Timestamped
  belongs_to :owner, User
  property :title, String
  property :message, String
  has_many :replies, Reply
end

Usage below.

Q: Is there a way to make sure that my database code can be used on any dbserver the user wants? I.E. psql, mySQL, oracle?

As always, do the Og.setup after declaring (or requireing) the Models. All you have to do, to change the DB, is to change the :store field of the hash.

Og.setup(
  :store => 'psql'
  # etc..
)

Other than that, Og tries to avoid to use special SQL syntax, so it will work on all the databases you named (well, not Oracle yet, but will at a later point as this is already planned). What you have to take care of, is what you write in Post.find(:sql =>....) since Og does not monitor what you write as plain SQL. Just use standard SQL92 and you should basically be fine.

Q: How do I use forms to interact with said data? Do I make a .xhtml in public with the same name as the defined page? And If I do that will they still work in sync?

Put .xhtmls in your Template folder, and yes, they will be reloaded when you change them while the server runs (if run in debug mode, live mode will not do this, so you have to restart the service then).

Q: After looking through the Rdoc of Og I saw Has_One and Has_many and other relationship classes, Should I use these? And How should I use them?

Yes, you should use them, as they make coding simpler, faster and less error prone. An example usage is stated below. The gain is, that one doesn't have to care on how those relations actually work, you just define that a forum has a few messages, which is the only thing you want to know.

f = Forum.new
f.name = 'test'
f.save

u = User.create
u.login = 'testuser'
# ...
u.save

p = Post.create
p.owner = u
p.message = "blabla \n blabla"
p.save

f.posts << p
f.save

r = Reply.create
r.owner = u
r.save

p.replies << r
#p.add_reply(r)
p.save

# example to query all messages in a Forum, 
# ordered by date:

messages = Forum.posts(:order_by => 'date')

End

See http://rubygarden.org/index.cgi/Libraries/og_tutorial.rdoc for an old but good tutorial.

Rating: 4