Skip to content
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

Optional relationships #2259

Open
wants to merge 2 commits into
base: 0-10-stable
Choose a base branch
from

Conversation

jacobbullock
Copy link

Purpose

With models that have many relationships, especially nested relationships, automatically including those relationships by default can make an otherwise fast query take a long time. Allowing an easy way to make relationships optional unless specifically requested through include is essential for performance

Changes

Only include relationships in the associations method if they are not defined as optional OR if they are optional and specified in the include request

There is also a small update to the .gitignore for excluding intelliJ project files

Caveats

This has worked perfectly for our existing needs including a number of polymorphic relationshisp, but may need further testing.

Related GitHub issues

#1865

Additional helpful information

In the following serializer:

class PostSerializer < ActiveModel::Serializer
    has_many :comments, :optional => true
    attributes :id, :title, :body
end

comments will not be the response unless asked for

render  :json => :post, :include  => "comments" # will include comments

render  :json => :post, # will only include post data

@bf4
Copy link
Member

bf4 commented Jun 22, 2018

Interesting idea. I assume this is for JSON:API? Would you consider using a 'minimal' serializer?

@richmolj
Copy link
Contributor

Not sure if it still exists in master, but I actually added exactly this in this PR.

(or use jsonapi suite and it's automatic 🙂 )

@jacobbullock
Copy link
Author

@richmolj that PR looks interesting, that looks more thought out than this. I couldn't find anything about that while I was searching for how to do this. I'll take a look and see if this works in lieu of what i posted here. I was going to use the jsonapi suite, but i like the auto serializing that AMS does that I believe is not in any of the other serializers.

@jacobbullock
Copy link
Author

@bf4 I am not quite sure I understand what you mean by 'minimal' serializer

@bf4
Copy link
Member

bf4 commented Jun 25, 2018

@bf4 I am not quite sure I understand what you mean by 'minimal' serializer

e.g.

class UserSerializer < ActiveModel::Serializer
  attribute :name
end
class UserPostsSerializer < UserSerializer
  has_many :posts
end
class UserAllTheThingsSerializer < UserSerializer
  has_many :posts
  belongs_to: :blog
  has_many :roles
  # etc
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants