def foreign_key_value(obj, relation, was = false)
      relation = relation.is_a?(Enumerable) ? relation.dup : [relation]
      first_relation = relation.first
      if was
        first = relation.shift
        foreign_key_value = attribute_was(obj, relation_foreign_key(first))
        klass = relation_klass(first, source: obj, was: was)
        if foreign_key_value
          value = klass.where(
            "#{klass.table_name}.#{relation_primary_key(first, source: obj, was: was)} = ?",
            foreign_key_value).first
        end
      else
        value = obj
      end
      while !value.nil? && relation.size > 0
        value = value.send(relation.shift)
      end
      return value.try(relation_primary_key(first_relation, source: obj, was: was).try(:to_sym))
    end
def foreign_key_value(obj, relation, was = false)
      relation = relation.is_a?(Enumerable) ? relation.dup : [relation]
      first_relation = relation.first
      if was
        first = relation.shift
        foreign_key_value = attribute_was(obj, relation_foreign_key(first))
        klass = relation_klass(first, source: obj, was: was)
        if foreign_key_value
          value = klass.where(
            "#{klass.table_name}.#{relation_primary_key(first, source: obj, was: was)} = ?",
            foreign_key_value).first
        end
      else
        value = obj
      end
      while !value.nil? && relation.size > 0
        value = value.send(relation.shift)
      end
      return value.try(relation_primary_key(first_relation, source: obj, was: was).try(:to_sym))
    end
def foreign_key_value ( obj , relation , was = false ) relation = relation . is_a? ( Enumerable ) ? relation . dup : [ relation ] first_relation = relation . first if was first = relation . shift foreign_key_value = attribute_was ( obj , relation_foreign_key ( first ) ) klass = relation_klass ( first , source : obj , was : was ) if foreign_key_value value = klass . where ( "#{klass.table_name}.#{relation_primary_key(first, source: obj, was: was)} = ?" , foreign_key_value ) . first end else value = obj end while ! value . nil? && relation . size > 0 value = value . send ( relation . shift ) end return value . try ( relation_primary_key ( first_relation , source : obj , was : was ) . try ( :to_sym ) ) end
gets the value of the foreign key on the given relation

 relation: a symbol or array of symbols; specifies the relation
   that has the counter cache column
 was: whether to get the current or past value from ActiveRecord;
   pass true to get the past value, false or nothing to get the
   current value
gets the value of the foreign key on the given relation