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
1 answer

Is it possible to run .to_json on a hash but not have it escape previously .to_json'ified strings?

We have a caching layer that stores json output of strings. I'd like to be able to put these strings into an array which I then transform to json via .to_json but it escapes all the previously encoded json. Is there a way to avoid this? Here's a…
timpone
  • 19,235
  • 36
  • 121
  • 211
0
votes
0 answers

Angular-Devise not working properly with UserSerializer (Active Model Serializer)

I am using the angular-devise Service in AngularJS to communicate with the Devise Authentication Gem in Rails. Everything (Login/Registration/Current_user/Facebook..) worked fine until I had to add this UserSerializer in Rails (Generated by Active…
0
votes
1 answer

Active Model Serializer remove relationship member when using JSON API

I'm using the latest AMS v0.10.0.rc3 with the JSON API adapter. So far is working great and is adding some useful conventions that i would like to change. For example, lets suppose that i have a Post serializer and a Comment serializer like…
Javier Cadiz
  • 12,326
  • 11
  • 55
  • 76
0
votes
1 answer

AMS transfer from 0.9 to 0.10. What's the best way to achieve embed: ids option (return ids of relationship)

I'm trying to run a project using new Active Model Serializer development (0.10rc3), What I found from logs - possibility to embed ids of relationship has been removed. In earlier versions of AMS it was possible to declare: class CompanySerializer <…
Mehal
  • 398
  • 3
  • 11
0
votes
1 answer

can't add a new key into hash during iteration for active_model_serializers 0.10.x

I am trying to upgrade to the active_model_serializers 0.10.x gem. But I am getting an error: can't add a new key into hash during iteration Here are the relevant portions of code: respond_to do |format| format.html # show.html.erb format.json…
ajayjapan
  • 349
  • 4
  • 18
0
votes
1 answer

Rails and ActiveModelSerializer: Merging two collections

I have two models ExpenseLog and IncomeLog with one serializer each (using active_model_serializers gem). I have a controller with a method that should return all the logs (both expensesLogs and incomesLogs) of the last 30 days. I have to merge both…
ascherman
  • 1,762
  • 2
  • 20
  • 41
0
votes
1 answer

add simple_format to an Active Model Serializer attribute

I'd like to simple_format the body of my CommentSerializer. I have: class CommentSerializer < ActiveModel::Serializer attributes :id, :body def body simple_format(body) end but this goes into a recursive call. Ideally, I'd like to keep…
timpone
  • 19,235
  • 36
  • 121
  • 211
0
votes
0 answers

Adding root value to a response from has_many_through relationship

I have a has_many through relationship in rails app. I is associated as follows: class Car < ActiveRecord::Base has_many :car_fuel_types has_many :fuel_types , through: :car_fuel_types end class FuelType < ActiveRecord::Base …
Lovish Choudhary
  • 167
  • 2
  • 17
0
votes
2 answers

active_model_serializer: How to test in the command line?

How can I check the serialization of an active model serializer in the command line? $ rails c > ModelSerializer.new(Model.last) => # does not give me the custom format of my serializer
steel
  • 11,883
  • 7
  • 72
  • 109
0
votes
1 answer

Order collection by first key value in serialized attribute

I have a Comment model with a serialized attribute: serialize :edit, Hash Each edit for the comment is timestamped by inserting a key/value pair: self.edit[DateTime.current] = edit_content How would I order a collection of comments, all sorted…
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
0
votes
1 answer

Scoping an associations on JSONAPI endpoint in Rails serializer

Is there a way to scope this endpoint (User#Show) on my Rails API, maybe in the serializer? I want to return only the user's last delivery_address, not all of them. I don't want it to effect non API requests. def show @user =…
Paul Byrne
  • 1,563
  • 18
  • 24
0
votes
1 answer

In ActiveModel::Serializer, how to render json-api relationships with id only

Using ActiveModel::Serializer (0.10.0.rc3) using the json-api adapter, how can I render relationships with id only by default? Consider my serializer class ASerializer < Api::V1::BaseSerializer attributes :id, :name has_many :bs end However,…
zeeMonkeez
  • 5,057
  • 3
  • 33
  • 56
0
votes
1 answer

Pass serializer: a String

In Active Model Serializers you can use a different Serializer by doing respond with resource, serializer: ViewContractsSerializer. The issue is that I want that to be dynamic by using serializer: "View#{resource_name}Serializer" and that does not…
Seldon Stone
  • 474
  • 1
  • 5
  • 16
0
votes
1 answer

Formatting Collection with Active Record Serializers

Total I have the following code: def search @regions = Region.search(params[:searchPhrase]).page(params[:current].to_i).per_page(params[:rowCount].to_i) data = { current: @regions.current_page, rowCount: @regions.per_page, total:…
lcguida
  • 3,787
  • 2
  • 33
  • 56
0
votes
0 answers

Use active model serializer with rails namespaces

I am in this situation, in my controller I call: render json: @customers_filtered, serializer: User::CustomerSerializer My serializer: class User::CustomerSerializer < ActiveModel::Serializer attributes :id,:firstname end But when I call the…
ciaoben
  • 3,138
  • 4
  • 27
  • 42