Archive for May, 2009

Pfingstturnier Wiesbaden.

Saturday, May 30th, 2009

Jedes Jahr an Pfingsten findet das Pfingstturnier im Schlosspark in Biebrich statt. Ich bin nun alles andere als ein Pferdemensch. Abgesehen von merkwürdigen Choreographien, bei denen Pferde und Menschen im (geordneten?) Chaos auf dem Reitplatz unterwegs sind, bietet der Abend aber immer wieder auch für Laien das eine oder andere Highlight.

Anbei einige Impressionen des gestrigen Abends.  Wie immer: Klick aufs Foto bringt Euch weiter.

Vielen Dank für die Blumen.

Wednesday, May 20th, 2009

Heute mal was schönes: Rosen, die ich am Rhein gesehen hab. Hatte auch die Kamera dabei und schwupps waren sie im Kasten. Wie immer: Bild anklicken und los gehts. Enjoy.

Rose am Rhein

Rails’ to_xml w/ multiple associations.

Thursday, May 14th, 2009

This is a pain and took me about an hour to figure out today.

Rails ActiveRecord instances offer a nice function to render xml:

my_model.to_xml()

This method can be fed with a parameter

:include => []

to have all associations being integrated in the XML tree, just like when calling

my_model = MyModel.find( :include => [association])

But when I tried to use it, it always failed when dealing with nested associations. Until I found out the trick.
This is how it works: Basically you need to build a list of nested hashes.

class Book < ActiveRecord::Base
  has_many :pages
  has_one :o wner
end
class Owner < ActiveRecord::Base
  has_many :books
end
class Page < ActiveRecord::Base
  has_many :spots_of_coffees
  belongs_to :book
end
class SpotOfCoffee < ActiveRecord::Base
  belongs_to :page
end

Now, you want to create a nice XML output containing a book, with its owner, pages and all spots of coffee?
Pretty easy:

  @book = Book.first
  includes = {} # let's build a hash; it's easier to read. At least for me...
  includes[:owner] = {} # owner has no association. So, let's take an empty hash as target
  includes[:pages] = { :include => :spots_of_coffee } #load pages and include its spots of coffee

  respond_to do |format|
    format.xml  { render :text => @book.to_xml(:include => includes) }
  end

Lustige Begegnung.

Friday, May 8th, 2009

Da geht man mal für zwei Stunden wandern am Kloster Eberbach und kommt an eine kleine “Farm” mit lustigen Tieren. Offensichtlich Lamas, mitten im Wald. Noch komischer war, dass das Fell der Tiere offensichtlich schon sommertauglich geschoren war. Bis auf den Kopf. Aber seht selbst.