The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Answering a hibernated cry for mercy

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
David Heinemeier Hansson

Posts: 512
Nickname: dhh
Registered: Mar, 2004

David Heinemeier Hansson is the lead Ruby developer on 37signal's Basecamp and constructor of Rails
Answering a hibernated cry for mercy Posted: Sep 4, 2004 4:43 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by David Heinemeier Hansson.
Original Post: Answering a hibernated cry for mercy
Feed Title: Loud Thinking
Feed URL: http://feeds.feedburner.com/LoudThinking
Feed Description: All about the full-stack, web-framework Rails for Ruby and on putting it to good effect with Basecamp
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by David Heinemeier Hansson
Latest Posts From Loud Thinking

Advertisement

informIT just started a new multiple-part tutorial on how to use Hibernate (the top dog in Java ORMs) to create a domain model for a small book shop. By the end of the second XML file, I was ready to cry for mercy. So I did and Active Record answered:


class Book < ActiveRecord::Base
  belongs_to :publisher
  has_and_belongs_to_many :authors
end
 
class Publisher < ActiveRecord::Base
  has_many :books
end
 
class Author < ActiveRecord::Base
  has_and_belongs_to_many :books
end

Whups. I think we just got ahead of ourselves. The model above won't materialize in the Hibernate tutorial for another few installments. For the first one, they just got Book up and running.

But since it took all of five seconds to describe the domain (please note that were assuming the existence of a database schema, just like the Hibernate tutorial), let's give a few examples on its usage as well:


pruby = Book.create(
  "title" => "Programming Ruby", 
  "price" => "20", 
  "published_on" => Date.new(2004, 10, 3)
)
 
pruby.title # => "Programming Ruby"
 
dthomas = Author.create("name" => "Dave Thomas")
pruby.authors << dthomas
 
pruby.has_authors? # => true
pruby.authors.first.name # => "Dave Thomas"
 
pragshelf = Publisher.create(
  "name" => "Pragmatic Bookshelf")
 
pruby.publisher = pragshelf
pruby.publisher # => "Pragmatic Bookshelf"
 
publisher.has_books? # => true
publisher.books.size # => 1
 
# Say "Hi, Dave Thomas"
puts "Hi, " + publisher.books.first.author.name 

This is not pseudo-code. It's how most of the domain logic in Basecamp and other Rails applications work. Why does it how to be harder than this?

If you're ready to get outta dodge, please do have a look at Rails. It's a web-application framework that includes Active Record and its sister Action Pack. I wouldn't recommend it if you're trying to make a living as a tutorials writer and you're paid by the word, though.

Read: Answering a hibernated cry for mercy

Topic: The Python (aka Ruby) Paradox Previous Topic   Next Topic Topic: Performance Quanten Sprung (Jump)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use