Thanks Emmanuel! I understand now and it works :) On Aug 31, 1:44 am, Emmanuel Gomez <[email protected]> wrote: > Hi Paul, > > On Aug 30, 2011, at 7:28 PM, Paul wrote: > > > > > > > > > > > Example: > > > class Project > > include DataMapper::Resource > > > has n, :tasks > > end > > > class Task > > include DataMapper::Resource > > .... > > end > > > Say I have object "p" (Project::Class) and object > > "association" (:tasks). How can I determine that "association" is of > > class Task and instantiate a Task object using datamapper? > > Every model (ie., class that includes DataMapper::Resource) has a > RelationshipSet, which can be queried for any of the relationships defined > for that model. This is done, as Jonathan pointed out, with > Model#relationships[:relationship_name], eg. > > project_to_tasks_relationship = Project.relationships[:tasks] # => > <#DataMapper::Associations::OneToMany::Relationship …> > > Every Relationship object represents one 'edge' of the conceptual > relationship (it is directed/directional). > > > In ActiveRecord, I can do the following: > > > new_object = p.class.reflect_on_association(association).klass.new > > > What is the Datamapper equivalent? > > As I mentioned above, a Relationship represents one direction (ie., edge) > originating at a source model, and can be queried for the target model 'at > the other end' (in graph terms, this is the other node, connected by the edge > represented by the Relationship object itself): > > Project.relationships[:tasks].target_model # => Task > > So to duplicate the effects of your example from ActiveRecord, you could do: > > relationship_name = :tasks > project = Project.new > new_task = project.model.relationships[relationship_name].target_model.new > > There are also other things you can do with a Relationship, all of which can > be found > athttps://github.com/datamapper/dm-core/blob/master/lib/dm-core/associa... > > Hope that helps, > Emmanuel
-- You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.
