mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
belongs_to causes unnecessary db hit
This commit is contained in:
parent
0f59d64ed5
commit
273b7e7f30
@ -137,6 +137,34 @@ module ActiveModel
|
|||||||
assert expected_association_keys.include? :site
|
assert expected_association_keys.include? :site
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class BelongsToBlogModel < ::Model
|
||||||
|
attributes :id, :title
|
||||||
|
associations :blog
|
||||||
|
end
|
||||||
|
class BelongsToBlogModelSerializer < ActiveModel::Serializer
|
||||||
|
type :posts
|
||||||
|
belongs_to :blog
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_belongs_to_doesnt_load_record
|
||||||
|
attributes = { id: 1, title: 'Belongs to Blog', blog: Blog.new(id: 5) }
|
||||||
|
post = BelongsToBlogModel.new(attributes)
|
||||||
|
class << post
|
||||||
|
def blog
|
||||||
|
fail 'should use blog_id'
|
||||||
|
end
|
||||||
|
|
||||||
|
def blog_id
|
||||||
|
5
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
actual = serializable(post, adapter: :json_api, serializer: BelongsToBlogModelSerializer).as_json
|
||||||
|
expected = { data: { id: '1', type: 'posts', relationships: { blog: { data: { id: '5', type: 'blogs' } } } } }
|
||||||
|
|
||||||
|
assert_equal expected, actual
|
||||||
|
end
|
||||||
|
|
||||||
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
|
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
|
||||||
has_many :comments
|
has_many :comments
|
||||||
has_many :comments, key: :last_comments do
|
has_many :comments, key: :last_comments do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user