Class: RPCMapper::Association::Has
- Defined in:
- lib/rpc_mapper/association.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #foreign_key ⇒ Object
- #polymorphic? ⇒ Boolean
-
#scope(object) ⇒ Object
Returns a scope on the target containing this association.
Methods inherited from Base
#collection?, #eager_loadable?, #initialize, #primary_key, #target_klass, #type
Constructor Details
This class inherits a constructor from RPCMapper::Association::Base
Instance Method Details
#foreign_key ⇒ Object
107 108 109 |
# File 'lib/rpc_mapper/association.rb', line 107 def foreign_key super || (self.polymorphic? ? "#{[:as]}_id" : "#{RPCMapper::Base.base_class_name(source_klass).downcase}_id").to_sym end |
#polymorphic? ⇒ Boolean
129 130 131 |
# File 'lib/rpc_mapper/association.rb', line 129 def polymorphic? !![:as] end |
#scope(object) ⇒ Object
Returns a scope on the target containing this association
Builds conditions on top of the base_scope generated from any finder options set with the association
has_many :widgets, :class_name => “Widget”, :foreign_key => :widget_id has_many :comments, :as => :parent
In addition to any finder options included with the association options the following will be added:
where( => source[:id])
Or for the polymorphic :comments association:
where(:parent_id => source[:id], :parent_type => source.class)
123 124 125 126 127 |
# File 'lib/rpc_mapper/association.rb', line 123 def scope(object) s = base_scope(object).where(self.foreign_key => object[self.primary_key]) if object[self.primary_key] s = s.where(:"#{[:as]}_type" => RPCMapper::Base.base_class_name(object.class)) if s && polymorphic? s end |