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

Add support for embed: :ids option for in associations #677

Merged
merged 1 commit into from
Oct 14, 2014
Merged

Add support for embed: :ids option for in associations #677

merged 1 commit into from
Oct 14, 2014

Conversation

guilleiguaran
Copy link
Member

Hey!!

We would like to add an option to associations to allow the inclusion of only associated object ids instead of full objects, for this I'm proposing the embed: :ids option, example:

class AuthorSerializer < ActiveModel::Serializer
  attributes :id, :name

  has_many :posts, embed: :ids
end

in JSON API adapter this will not include the posts within the linked hash.

I'm not convinced by the option name, let me know if you have other suggestions.

@kurko
Copy link
Member

kurko commented Oct 10, 2014

This looks good to me. I'm thinking about something like the following would be helpful too:

class CompanySerializer < ActiveModel::Serializer
  attributes :id, :name

  has_many :employees, embed: :employees_ids
  has_many :addresses, embed: :ids

  private

  def employees_ids
    self.employees.pluck(:id) unless self.employees.count > 500
  end
end

@sogamoso
Copy link

+1 for this.

@kurko's suggestion would be really too, although that could be part of a later pull request.

steveklabnik added a commit that referenced this pull request Oct 14, 2014
Add support for embed: :ids option for in associations
@steveklabnik steveklabnik merged commit 410eacc into rails-api:master Oct 14, 2014
@steveklabnik
Copy link
Contributor

I like the name :)

@sogamoso sogamoso deleted the embed-ids-option branch October 14, 2014 18:15
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.

4 participants