Skip to content

belongs_to causes unnecessary db hits #1100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ def test_associations_custom_keys
assert expected_association_keys.include? :site
end

class BelongsToTestPostSerializer < ActiveModel::Serializer
belongs_to :blog
end

def test_belongs_to_doesnt_load_record
post = Post.new
post.blog_id = 'blog'

class << post
def blog
fail 'should use blog_id'
end
end

actual = serializable(post, adapter: :json_api, serializer: BelongsToTestPostSerializer).as_json
expected = { data: { id: 'post', type: 'posts', relationships: { blog: { data: { id: 'blog', type: 'blogs' } } } } }

assert_equal expected, actual
end

class InlineAssociationTestPostSerializer < ActiveModel::Serializer
has_many :comments
has_many :comments, key: :last_comments do
Expand Down