Questions tagged [active-model-serializers]

The purpose of `ActiveModel::Serializers` is to provide an object to encapsulate serialization of `ActiveModel` objects, including `ActiveRecord` objects.

Purpose

The purpose of ActiveModel::Serializers is to provide an object to encapsulate serialization of ActiveModel objects, including ActiveRecord objects.

Serializers know about both a model and the current_user, so you can customize serialization based upon whether a user is authorized to see the content.

In short, serializers replaces hash-driven development with object-oriented development.

Documentation

See the github project's README

739 questions
0
votes
2 answers

In Rails, how can you return model data from multiple different ActiveModelSerializers/Models at once?

I have a couple models. Let's call them Widget and Gadget. My #index for for Widget and Gadget looks something like this def index widgets = Widget.all if widgets respond_with widgets, each_serializer: Api::V1::WidgetSerializer …
chris P
  • 6,359
  • 11
  • 40
  • 84
0
votes
1 answer

Using Rails Serializers (active_model_serializers) to render key/value pairs based on incremental primary keys

I'm trying to format the serialized output from the active_model_serializers gem as key/value pairs. By default, ActiveModel::Serializer renders an output like this: [ { "id": 1, "value": "foo" }, { "id": 2, "value": "bar" …
Synthead
  • 2,162
  • 5
  • 22
  • 25
0
votes
1 answer

Load associations to one level while conditionally sideloading associations in Active model serializers

AMS version 0.8.3, I created a base_serializer.rb like this and extended the same. class BaseSerializer < ActiveModel::Serializer def include_associations! if @options[:embed] embed = @options[:embed].split(',').map{|item|…
0
votes
1 answer

Stack level too deep, belongs_to/has_many relationship in Rails API and active_model_serializer

I have a simple relationship set up between two models, collections and works. A collection has many works, and a work belongs to a collection. The collection serializer: #collection_serializer.rb class CollectionSerializer <…
amhasler
  • 67
  • 1
  • 6
0
votes
1 answer

Active model serializer association unless object.user == current_user

I have a User model and an Employment model like so: Class User has_many :employments Class Employment belongs_to :user I'm serving up a JSON api via Active model serializer like so: class EmploymentSerializer < ActiveModel::Serializer …
Ryan.lay
  • 1,741
  • 2
  • 17
  • 30
0
votes
1 answer

Trying to eval for null value and insert link to google search when value is null

I have a Kendo grid that is being binded to a remote data source. I would like to insert a link to a google search when values in a specific column is Null. <%= company_URL || link_to("Search",…
0
votes
1 answer

Editing model with Ember-data and Rails not saving

I am attempting to edit a user with ember-data and rails, when I edit the user information and try to do a model.save() I get a unexpected end of input error in my console. It looks like there is a weird query going on in the Rails side. Here is…
0
votes
1 answer

Migrating ActiveModel::Serializer down from v0.9.2 to 0.8, has_many not being serialised

I have the following ActiveModel::Serializer class class MyThingySerializer < ActiveModel::Serializer root false attributes :id, :name, :description has_many :whatsits, embed_namespace: :_embedded delegate :whatsits, to: :object end It…
Dave Sag
  • 13,266
  • 14
  • 86
  • 134
0
votes
1 answer

Rails 4.2 Active Record Serializer JSON format issue

I have a serializer on rails that has another nested serializer inside like this class FeedSerializer < ActiveModel::Serializer attributes :id has_one :user def user if object.anonymous nil else …
0
votes
1 answer

How can I render/respond_with a record that's is from an unrelated model/controller?

I'm using ActiveModelSerializers for much of my JSON API. I have a User model, users_controller, and user_serializer for example. In my users_controller, I render my json via the following... user = User.find_by_id(param[:id]) respond_with user,…
chris P
  • 6,359
  • 11
  • 40
  • 84
0
votes
1 answer

Active Model Serializer send parent of has_many

In my Comments controller, I serialize the comments. When I put a belongs_to :post_id in my serializer, every comment has a post with it, but since all of these comments come from the same post, it is redundant. I know I can use the post serializer…
David
  • 7,028
  • 10
  • 48
  • 95
0
votes
1 answer

Using ActiveModel::Serializer with a model that has several references onto the same object

I'm trying to create a serializer that will give out an object that has several references onto the same object. For example there is an "Lender" object that has two addresses, one of them is the "registration address" and the second is "actual…
AlexanderT
  • 11
  • 4
0
votes
1 answer

JSON.parse conflicting with ActiveModel::Serializers::JSON::Module?

I am parsing JSON returned from Google places in one of my models. It ran fine until I started implementing ActiveModel Serializers, and now I'm getting this error: undefined method `parse' for ActiveModel::Serializers::JSON:Module The even…
scottier
  • 248
  • 2
  • 10
0
votes
2 answers

ActiveModel::Serializer include attribute prefix?

I am using AMS to comply with an older API and attempting to include a prefix on each attribute. Suppose i have this serializer: InvoiceSerializer.new(invoice).serializable_hash => { :id=>662473, :number=>"3817", :created_at=>Tue, 27 Jan 2015…
Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
0
votes
1 answer

ruby active model serializer association on version > 0.8.0

How do I get this relation to work with the new version? class UserSerializer < ActiveModel::Serializer embed :ids attributes :id, :email, :created_at, :updated_at, :auth_token has_many :products end works with gem…
Stefflan00
  • 155
  • 1
  • 1
  • 8