lib/counter_culture.rb
# 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
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 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
def foreign_key_value(relation, was = false)
      relation = relation.is_a?(Enumerable) ? relation.dup : [relation]
      first_relation = relation.first
      if was
        first = relation.shift
        foreign_key_value = send("#{relation_foreign_key(first)}_was")
        klass = relation_klass(first)
        value = klass.where("#{klass.table_name}.#{relation_primary_key(first)} = ?", foreign_key_value).first if foreign_key_value
      else
        value = self
      end
      while !value.nil? && relation.size > 0
        value = value.send(relation.shift)
      end
      return value.try(relation_primary_key(first_relation).to_sym)
    end
def foreign_key_value ( relation , was = false ) relation = relation . is_a? ( Enumerable ) ? relation . dup : [ relation ] first_relation = relation . first if was first = relation . shift foreign_key_value = send ( "#{relation_foreign_key(first)}_was" ) klass = relation_klass ( first ) value = klass . where ( "#{klass.table_name}.#{relation_primary_key(first)} = ?" , foreign_key_value ) . first if foreign_key_value else value = self end while ! value . nil? && relation . size > 0 value = value . send ( relation . shift ) end return value . try ( relation_primary_key ( first_relation ) . 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 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
relation
<not_specific>
was
<not_specific>