dont create a object for a has one relationship unnecessarily just to fetch id

This commit is contained in:
Shishir Kakaraddi 2018-05-14 20:15:49 -07:00 committed by Shishir Kakaraddi
parent a018f1d32f
commit cd1bc0968e
3 changed files with 14 additions and 6 deletions

View File

@ -185,11 +185,7 @@ module FastJsonapi
return object.id return object.id
end end
if relationship[:relationship_type] == :has_one record.public_send(relationship[:id_method_name])
record.public_send(relationship[:object_method_name]).try(:id)
else
record.public_send(relationship[:id_method_name])
end
end end
end end
end end

View File

@ -30,6 +30,10 @@ describe FastJsonapi::ObjectSerializer do
p.user_id = id p.user_id = id
p p
end end
def photo_id
1
end
end end
class UserSerializer class UserSerializer
@ -77,6 +81,10 @@ describe FastJsonapi::ObjectSerializer do
a.employee_id = id a.employee_id = id
a a
end end
def account_id
1
end
end end
class EmployeeSerializer < UserSerializer class EmployeeSerializer < UserSerializer

View File

@ -31,6 +31,10 @@ RSpec.shared_context 'movie class' do
mt mt
end end
def advertising_campaign_id
1
end
def advertising_campaign def advertising_campaign
ac = AdvertisingCampaign.new ac = AdvertisingCampaign.new
ac.id = 1 ac.id = 1
@ -284,7 +288,7 @@ RSpec.shared_context 'movie class' do
:owner_id, :owner_id,
:owner, :owner,
:movie_type_id, :movie_type_id,
:advertising_campaign :advertising_campaign_id
) )
ActorStruct = Struct.new(:id, :name, :email, :agency_id, :award_ids) ActorStruct = Struct.new(:id, :name, :email, :agency_id, :award_ids)