Skip to content
Justin Coyne edited this page May 28, 2013 · 7 revisions

Conventions

A has_many B / B belongs_to A

class A < ActiveFedora::Base
  has_many :parts, :property => :is_part_of, :class_name => 'B'

class B < ActiveFedora::Base
  belongs_to :container, :property => :is_part_of, :class_name => 'A'

Examples

Component belongs_to Item / Item has_many Components

A one-directional isPartOf relationship from Component -> Item such that a Component belongs to one and only one Item, and an Item has many (0 or more) Component parts (but does not have a hasPart relationship in Fedora to the parts).

Component model:

class Component < ActiveFedora::Base
  belongs_to :container, :property => :is_part_of, :class_name => 'Item'
end

Item model:

class Item < ActiveFedora::Base
  has_many :parts, :property => :is_part_of, :class_name => 'Component'
end

Examples from Rails console

Add relationship from Item side: append Component to i.parts (don't have to save Item to persist relationship in Fedora)

>> c = Component.create
 => #<Component:-3317198817906593687 @pid="changeme:7" > 
>> i = Item.create
 => #<Item:-4257014214250004127 @pid="changeme:8" > 
>> i.parts << c
 => [#<Component:-3317198817906593687 @pid="changeme:7" >] 
>> c.container
 => #<Item:-3138316514265547563 @pid="changeme:8" > 

Add relationship from Component side: set c.container to Item and save c to persist relationship in Fedora:

>> c = Component.create
 => #<Component:-2077553887768847654 @pid="changeme:9" > 
>> i = Item.create
 => #<Item:-2107299566372493906 @pid="changeme:10" > 
>> c.container = i
 => #<Item:-2107299566372493906 @pid="changeme:10" > 
>> c.container # NOT YET PERSISTED TO FEDORA!
 => #<Item:-2107299566372493906 @pid="changeme:10" > 
>> c.save # PERSISTED TO FEDORA
 => true 
>> c2 = Component.find(c.pid)
 => #<Component:4140166978314351982 @pid="changeme:9" > 
>> c2.container
 => #<Item:-4009734262823123442 @pid="changeme:10" > 
>> i.parts
 => [#<Component:-1494282201889201822 @pid="changeme:9" >] 

Custom Relationships

ActiveFedora's default relationships are defined in config/predicate_mappings.yml of the active-fedora gem. In order to add a new relationship, copy predicate_mappings.yml to the config/ directory of your code base. The comments at the top of the file explain how to add new entries.

Clone this wiki locally