Module: ActiveFedora::Associations::ClassMethods

Defined in:
lib/active_fedora/associations.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(association_id, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/active_fedora/associations.rb', line 43

def belongs_to(association_id, options = {})
  raise "You must specify a property name for #{name}" if !options[:property]
  has_relationship association_id.to_s, options[:property]
  reflection = create_belongs_to_reflection(association_id, options)

  association_accessor_methods(reflection, BelongsToAssociation)
    # association_constructor_method(:build,  reflection, BelongsToAssociation)
    # association_constructor_method(:create, reflection, BelongsToAssociation)
  #configure_dependency_for_belongs_to(reflection)
end

#has_and_belongs_to_many(association_id, options = {}, &extension) ⇒ Object

Specifies a many-to-many relationship with another class. The relatioship is written to both classes simultaneously.

Adds the following methods for retrieval and query:

collection(force_reload = false)

Returns an array of all the associated objects. An empty array is returned if none are found.

collection<<(object, …)

Adds one or more objects to the collection by creating associations in the join table (collection.push and collection.concat are aliases to this method). Note that this operation instantly fires update sql without waiting for the save or update call on the parent object.

collection.delete(object, …)

Removes one or more objects from the collection by removing their associations from the join table. This does not destroy the objects.

collection=objects

Replaces the collection’s content by deleting and adding objects as appropriate.

collection_singular_ids

Returns an array of the associated objects’ ids.

collection_singular_ids=ids

Replace the collection by the objects identified by the primary keys in ids.

collection.clear

Removes every object from the collection. This does not destroy the objects.

collection.empty?

Returns true if there are no associated objects.

collection.size

Returns the number of associated objects.

(collection is replaced with the symbol passed as the first argument, so has_and_belongs_to_many :categories would add among others categories.empty?.)

Example

A Developer class declares has_and_belongs_to_many :projects, which will add:

  • Developer#projects

  • Developer#projects<<

  • Developer#projects.delete

  • Developer#projects=

  • Developer#project_ids

  • Developer#project_ids=

  • Developer#projects.clear

  • Developer#projects.empty?

  • Developer#projects.size

  • Developer#projects.find(id)

  • Developer#projects.exists?(...)

The declaration may include an options hash to specialize the behavior of the association.

Options

:class_name

Specify the class name of the association. Use it only if that name can’t be inferred from the association name. So has_and_belongs_to_many :projects will by default be linked to the Project class, but if the real class name is SuperProject, you’ll have to specify it with this option.

:property

REQUIRED Specify the predicate to use when storing the relationship.

Option examples:

has_and_belongs_to_many :projects, :property=>:works_on
has_and_belongs_to_many :nations, :class_name => "Country", :property=>:is_citizen_of


114
115
116
117
118
119
# File 'lib/active_fedora/associations.rb', line 114

def has_and_belongs_to_many(association_id, options = {}, &extension)
  reflection = create_has_and_belongs_to_many_reflection(association_id, options, &extension)
  collection_accessor_methods(reflection, HasAndBelongsToManyAssociation)
  #configure_after_destroy_method_for_has_and_belongs_to_many(reflection)
  #add_association_callbacks(reflection.name, options)
end

#has_many(association_id, options = {}) ⇒ Object



35
36
37
38
39
40
# File 'lib/active_fedora/associations.rb', line 35

def has_many(association_id, options={})
  raise "You must specify a property name for #{name}" if !options[:property]
  has_relationship association_id.to_s, options[:property], :inbound => true
  reflection = create_has_many_reflection(association_id, options)
  collection_accessor_methods(reflection, HasManyAssociation)
end