Module: StElsewhere
- Defined in:
- lib/st-elsewhere.rb
Instance Method Summary collapse
- #associations_to_association_ids(associations) ⇒ Object
-
#has_many_elsewhere(association_id, options = {}, &extension) ⇒ Object
Specifies a one-to-many association across database connections.
Instance Method Details
#associations_to_association_ids(associations) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/st-elsewhere.rb', line 107 def associations_to_association_ids(associations) ids = [] if associations && !associations.empty? associations.reject!{|a| a.to_s.empty? } association_class = associations.first.class.to_s ids = case association_class when "String" associations.map{|a| a.to_i } when "Fixnum" associations else associations.map{|a| a.id} end end ids end |
#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=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
39 40 41 42 43 44 |
# File 'lib/st-elsewhere.rb', line 39 def has_many_elsewhere(association_id, = {}, &extension) association_class = association_id.to_s.classify.constantize through = [: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 |