Module: StElsewhere

Defined in:
lib/st-elsewhere.rb

Instance Method Summary collapse

Instance Method Details

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

Specifies a one-to-many association across database connections. This is currently an incomplete implementation and does not yet use all of the options supported by has_many

The following methods for retrieval and query of collections of associated objects will be added:

collection<<(object, …)

TODO: Adds one or more objects to the collection by setting their foreign keys to the collection’s primary key.

collection=objects

Replaces the collections 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 with the objects identified by the primary keys in ids

collection.empty?

Returns true if there are no associated objects.

collection.size

Returns the number of associated objects.

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

Example

Example: A Firm class declares has_many_elsewhere :clients, which will add:

  • Firm#clients (similar to Clients.find :all, :conditions => ["firm_id = ?", id])

  • Firm#clients=

  • Firm#client_ids

  • Firm#client_ids=

  • Firm#clients.empty? (similar to firm.clients.size == 0)

  • Firm#clients.size (similar to Client.count "firm_id = #{id}")

Supported options

:through

Specifies a Join Model through which to perform the query. You can only use a :through query through a belongs_to has_one or has_many association on the join model.

Option examples:

has_many_elsewhere :subscribers, :through => :subscriptions

Raises:

  • (ArgumentError)


41
42
43
44
45
46
# File 'lib/st-elsewhere.rb', line 41

def has_many_elsewhere(association_id, options = {}, &extension)
  association_class = association_id.to_s.classify.constantize
  through = options[:through]
  raise ArgumentError.new("You must include :through => association for has_many_elsewhere") if not through
  collection_accessor_methods_elsewhere(association_id, association_class, through)
end