How to belongs to many (not habtm)

Well, if the same picture can belong to an Event AND a biography at the
same time, it gets kinda complicated, but if one picture can only belon
to an event OR a biography, the simple solution is a polymorphoc

class Biography < ActiveRecord::Base
has_many :pictures, :as => :image

class event < ActiveRecord::Base
has_manny :pictures, :as => :image

class Picture < ActiveRecord::Base
belongs_to :image, :polymorphic => true

Now your Picture Model only needs 2 special fields:

image_id (Integer) = the foreign key of the object it belongs_to
image_type (string) = the Class Name of the object it belongs_to

those 2 fields are handled by ActiveRecord, you dont have to provide
the values yourself ...

now you can do: # get all Pictures of an event
... OR ...
@event =
... ETC ..
and all the other stuff you could do with a simple
has_many <-> belongs_to relationship.

Read more here:

Theres also a way for polymorphic habtm relationships, and an example
in the wiki, but i didnt fully understand it yet :smiley: