Merge pull request #176 from hmsimple/id-methods

Use pluck(:id) or _id methods instead of associated objects
This commit is contained in:
Steve Klabnik 2012-12-20 11:55:57 -08:00
commit 89660d1c5d

View File

@ -109,12 +109,15 @@ module ActiveModel
def serialize_ids def serialize_ids
# Use pluck or select_columns if available # Use pluck or select_columns if available
# return collection.ids if collection.respond_to?(:ids) # return collection.ids if collection.respond_to?(:ids)
if !option(:include) && associated_object.respond_to?(:pluck)
associated_object.pluck(:id)
else
associated_object.map do |item| associated_object.map do |item|
item.read_attribute_for_serialization(:id) item.read_attribute_for_serialization(:id)
end end
end end
end end
end
class HasOne < Config #:nodoc: class HasOne < Config #:nodoc:
def embeddable? def embeddable?
@ -161,15 +164,19 @@ module ActiveModel
end end
def serialize_ids def serialize_ids
object = associated_object if polymorphic?
if associated_object
if object && polymorphic?
{ {
:type => polymorphic_key, :type => polymorphic_key,
:id => object.read_attribute_for_serialization(:id) :id => associated_object.read_attribute_for_serialization(:id)
} }
elsif object else
object.read_attribute_for_serialization(:id) nil
end
elsif source_serializer.object.respond_to?("#{name}_id")
source_serializer.object.send("#{name}_id")
elsif associated_object
associated_object.read_attribute_for_serialization(:id)
else else
nil nil
end end