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
association.
class Biography < ActiveRecord::Base
has_many :pictures, :as => :image
end
class event < ActiveRecord::Base
has_manny :pictures, :as => :image
end
class Picture < ActiveRecord::Base
belongs_to :image, :polymorphic => true
end
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:
@event.pictures # get all Pictures of an event
... OR ...
@event = Event.pictures.build(someParaamsHere)
@event.save
... ETC ..
and all the other stuff you could do with a simple
has_many <-> belongs_to relationship.
Read more here:
http://wiki.rubyonrails.org/rails/pages/PolymorphicAssociations
Theres also a way for polymorphic habtm relationships, and an example
in the wiki, but i didnt fully understand it yet :D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---