Class: Perry::Association::BelongsTo

Inherits:
Base
  • Object
show all
Defined in:
lib/perry/association.rb

Instance Attribute Summary

Attributes inherited from Base

#id, #options, #source_klass

Instance Method Summary collapse

Methods inherited from Base

#eager_loadable?, #initialize, #primary_key, #target_klass

Constructor Details

This class inherits a constructor from Perry::Association::Base

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/perry/association.rb', line 144

def collection?
  false
end

#foreign_keyObject



148
149
150
# File 'lib/perry/association.rb', line 148

def foreign_key
  super || "#{id}_id".to_sym
end

#polymorphic?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/perry/association.rb', line 152

def polymorphic?
  !!options[:polymorphic]
end

#polymorphic_typeObject



156
157
158
# File 'lib/perry/association.rb', line 156

def polymorphic_type
  "#{id}_type".to_sym
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

belongs_to :foo, :foreign_key => :foo_id

In addition to any finder options included with the association options the following scope will be added:

where(:id => source[:foo_id])


172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/perry/association.rb', line 172

def scope(object)
  if object.is_a? Array
    keys = object.collect(&self.foreign_key.to_sym)
    keys = nil if keys.empty?
  else
    keys = object[self.foreign_key]
  end
  if keys
    scope = base_scope(object)
    scope.where(self.primary_key(object) => keys)
  end
end

#typeObject



140
141
142
# File 'lib/perry/association.rb', line 140

def type
  :belongs_to
end