ActiveModelSerializer not serialize object nested children - ruby-on-rails

I have a model Category that use awesome_nested_set gem, so it has children of the model itself. I have created CategorySerializer for the model
class CategorySerializer < ActiveModel::Serializer
attributes :id, :parent_id, :lft, :rgt, :text, :permalink, :children
def children
object.children
end
end
But children is not serialized. I have also tried add has_many :children, serializer: self, the result is this
{
"id": 25918,
"parent_id": null,
"lft": 3,
"rgt": 8,
"text": "ARAG",
"permalink": "25918-arag",
"children": [
{
"id": 25919,
"parent_id": 25918,
"lft": 4,
"rgt": 7,
"text": "Coperchi",
"permalink": "25919-coperchi",
"children": [
{
"id": 25920,
"parent_id": 25919,
"lft": 5,
"rgt": 6,
"text": "Ribaltabili",
"description": "",
"page_title": "",
"meta_key": "",
"meta_description": "",
"key_1": null,
"key_2": null,
"key_3": null,
"extra": null,
"created_at": "2019-03-01T21:08:15.000+01:00",
"updated_at": "2019-04-02T12:27:05.000+02:00"
}
]
}
]
}
Second level of children is successfully serialized but it children is not. Is there a way or alternative to serialize all object children?

If you want deep nesting by default, then you can set following config property in the initializer file
# config/initializers/active_model_serializer.rb
ActiveModelSerializers.config.default_includes = '**
For more details, you can check this.
You can also add another serializer for children as follow
class CategorySerializer < ActiveModel::Serializer
attributes :id, :parent_id, :lft, :rgt, :text, :permalink, :children
def children
ActiveModel::SerializableResource.new(object.children, each_serializer: ChildrenSerializer)
end
end
For more information, you can refer to this link

How about this solution with invoking serializer on children? Be careful to not get into infinite loop with deep nesting
def children
object.children.map { |obj| SomeSerializer.new(obj) }
end

Related

How Can I permit nested attributes with active model serializer?

I'm using active_model_serializers from an api in ruby on rails, and I have a controller method in which i save an invoice and its nested items with some calculations, the problem is that after include serializer, the nested attributes are restricted and I can't access to them.
I have the code in this way according to some documentation, but it doesn't works
#Controller
def invoice_params
params.require(:invoice).permit(:person_id, :date, sales_attributes: [:reference_id, :quantity, :price])
end
#Model
class Invoice < ApplicationRecord
belongs_to :person
has_many :sales
accepts_nested_attributes_for :sales, allow_destroy: true
end
#Serializer
class InvoiceSerializer < ActiveModel::Serializer
attributes :id, :date, :total, :profit, :consecutive, :person_id
has_many :sales, root: :sales_attributes
belongs_to :person
end
The json request that I'm sending is:
{
"person_id": 4,
"date": "2019-03-20",
"sales": [
{
"reference_id":1,
"quantity": 90000,
"price": 240
},
{
"reference_id":1,
"quantity": 50000,
"price": 240
}
]
}
Some one knows what happen?, before of include the serializer gem it was working fine.
Thanks in advance!
change
def invoice_params
params.require(:invoice).permit(:person_id, :date, sales_attributes: [:reference_id, :quantity, :price])
end
to
def invoice_params
params.require(:invoice).permit(:person_id, :date, sales_attributes: [:id, :reference_id, :quantity, :price])
end
and
{
"person_id": 4,
"date": "2019-03-20",
"sales": [
{
"reference_id":1,
"quantity": 90000,
"price": 240
},
{
"reference_id":1,
"quantity": 50000,
"price": 240
}
]
}
to
{
"person_id": 4,
"date": "2019-03-20",
"sales_attributes": [
{
"reference_id":1,
"quantity": 90000,
"price": 240
},
{
"reference_id":1,
"quantity": 50000,
"price": 240
}
]
}

Rails Active Model Serializer include in controller not injecting has many relationship in included parameter

I'm seeing a strange behaviour regarding rails 5, active model serializer and the json-api adapter.
Given the following User model with the Rolify gem:
class User < ActiveRecord::Base
#
# Gem Includes
#
rolify
# Include devise modules.
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
include DeviseTokenAuth::Concerns::User
#
# Callbacks
#
after_create :assign_default_role
#
# Attributes
#
attr_accessor :remote_image
#
# Validations
#
validates :name, presence: true, length: {in: 1..100}
validates :last_name, presence: true, length: {in: 1..100}
validates :role_ids, presence: true, on: :update
#
# Relations
#
belongs_to :current_scenario, class_name: "Scenario"
#
# Private Instance Methods
#
def assign_default_role
self.add_role(:user) if self.roles.blank?
end
end
and the following controller code:
def show
#user = User.find(params[:id])
authorize #user
render json: #user, include: ['roles'], status: :ok
end
As you can see, I'm including the roles relationship to be rendered as part of the json api response, with json-api adapter format.
FYI, the UserSerializer:
class UserSerializer < ActiveModel::Serializer
#
# Attributes
#
attributes :id, :email, :name, :last_name, :image_url, :image_thumb_url, :created_at, :updated_at, :current_scenario_id, :last_sign_in_at
#
# Relations
#
has_one :current_scenario
has_many :roles
#
# Methods
#
def image_url
object.image_url
end
def image_thumb_url
object.image_url(:thumb)
end
end
When retrieving the json response, I get the following:
{
"data": {
"id":"2",
"type":"users",
"attributes": {
"email":"talvarez#igaltex.com.ar", ...
},
"relationships": {
"current-scenario": {
"data": {
"id":"204",
"type":"scenarios"
}
},
"roles": {
"data": [
{
"id":1,
"name":"user",
"resource-type":null,
"resource-id":null,
"created-at":"2017-01-23T10:27:08.707-03:00",
"updated-at":"2017-01-23T10:27:08.707-03:00"
},
{
"id":2,
"name":"admin",
"resource-type":null,
"resource-id":null,
"created-at":"2017-01-24T09:40:53.020-03:00",
"updated-at":"2017-01-24T09:40:53.020-03:00"
}
]
}
}
}
}
As you can see, the included relationship roles with all its attributes is inside the relationships fragment of the json-api response. Shouldn't the roles data be inside the included fragment, which by the way is missing? Moreover inside the relationship fragment roles should appear only as a reference like: {relationships: {roles: [{id: "1", type: "role"}, {id: "2", type: "role"}]} am I wrong?
To contrast this, look what happens when also including the current_scenario relationship:
{
"data": {
"id":"2",
"type":"users",
"attributes": {
"email":"talvarez#igaltex.com.ar",
"name":"Tomás",
"last-name":"Alvarez",
...
},
"relationships": {
"current-scenario": {
"data": {
"id":"204",
"type":"scenarios"
}
},
"roles": {
"data": [
{
"id":1,
"name":"user",
"resource-type":null,
...
}
]
}
},
"included": [
{
"id":"204",
"type":"scenarios",
"attributes": {
"name":"Scenario reload II",
"description":null,
"created-at":"2017-04-18T11:55:35.242-03:00",
"updated-at":"2017-04-18T11:55:35.242-03:00"
},
"relationships": {
"scenario-stocks": {
"data":[]
}
}
}
]
}
}
See how now the included fragment appears with all the information about current_scenario and only the reference to current_scenario is added to the relationships fragment. Is this because roles is a has_many relationship in the active model serializer while current_scenario is a belongs_to ? Am I understanding wrong the json-api adapter specification?
Many thanks!
Ouch. The inconsistency in the JSON-API response was because i forgot to add a Role model serializer in the backend side (Rails 5). This is the json response now, which is what i was looking for:
{
"data": {
"id": "2",
"type": "users",
"attributes": {
"email": "talvarez#igaltex.com.ar",
"name": "Tomás",
"last-name": "Alvarez",
"image-url": "http://localhost:3001/uploads/user/image/2/05a4dc7.jpg",
"image-thumb-url": "http://localhost:3001/uploads/user/image/2/thumb_05a4dc7.jpg",
"created-at": "2017-01-23T10:39:12.485-03:00",
"updated-at": "2017-04-25T16:32:14.610-03:00",
"current-scenario-id": 204,
"last-sign-in-at": "2017-04-25T16:29:03.559-03:00"
},
"relationships": {
"current-scenario": {
"data": {
"id": "204",
"type": "scenarios"
}
},
"roles": {
"data": [{
"id": "1",
"type": "roles"
}, {
"id": "2",
"type": "roles"
}]
}
}
},
"included": [{
"id": "204",
"type": "scenarios",
"attributes": {
"name": "Scenario reload II",
"description": null,
"created-at": "2017-04-18T11:55:35.242-03:00",
"updated-at": "2017-04-18T11:55:35.242-03:00"
},
"relationships": {
"scenario-stocks": {
"data": []
}
}
}, {
"id": "1",
"type": "roles",
"attributes": {
"name": "user"
}
}, {
"id": "2",
"type": "roles",
"attributes": {
"name": "admin"
}
}]
}
Sorry for this bug. We haven't figured out how to determine the type for a relationship when no serializer is found. Would it have been more helpful if you got an exception?
This is how JSON API works you can not retrieve desired model + relationship in 'one object'. Relationships always are separated. So you need to 'glue' it somehow. You can use gem that can help you with it, or you can do it on front end side (all big frameworks support it).
In general this approach with 'relationships' looks weird, but when you have complex object with tons of dependencies, this is the only one way that works.

rails multiple .each issue

I am using mongoid with my rails and am having a slight issue with running multiple .each
I have 3 models: Users, `Places', and 'Posts'
Users and Places both has_many Posts and Posts belongs_to both Users and Places
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paperclip
has_many :posts
}
class Place
include Mongoid::Document
include Mongoid::Timestamps
has_many :posts
}
class Post
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paperclip
belongs_to :place
belongs_to :user
}
i have a method on my user model to get all the users within my friends, including myself like so:
def self.as_objects_with_self(id)
friends = Array.new
self.as_ids_with_self(id).each do | friend |
friends << User.find(friend)
end
friends
end
This returns back an array of all my friends including myself.
What I am trying to do, is get a list of my friends and myself, and under each user have a list of their posts, and within each post have a reference to the place it is tied to... most of this is working
resources = Friendship.as_objects_with_self(current_user.id)
resources.each do |resource|
if resource.posts.count > 0
resource[:posts] = resource.posts.all
resource[:posts].each do |post|
post[:place] = Place.find(post[:place])
end
end
end
The only part to this that isn't working, is the .each on my :posts. I can see in the logs that it's searching the place table, but nothing is being added to the collection.
It adds this output:
[
{
"_id": "556e26844a75730453170000",
"authentication_token": "Q6R4FNs5i3n1-zfQfbp8",
"created_at": "2015-06-02T21:56:20.684Z",
"dob": "1982-08-01",
"email": "test1#gmail.com",
"gender": "Male",
"last_action": null,
"name": "tester1",
"picture_content_type": null,
"picture_file_name": null,
"picture_file_size": null,
"picture_fingerprint": null,
"picture_updated_at": null,
"picture_url": "/pictures/original/missing.png",
"posts": [
{
"_id": "556e32b54a75730453270000",
"created_at": "2015-06-02T22:48:21.962Z",
"media_content_type": null,
"media_file_name": null,
"media_file_size": null,
"media_fingerprint": null,
"media_updated_at": null,
"picture_content_type": "image/jpeg",
"picture_file_name": "8580243774_479d5fe7bf_z.jpg",
"picture_file_size": 191938,
"picture_fingerprint": "61bdc1d21158c76d601b028bf826b437",
"picture_updated_at": "2015-06-02T22:48:21.768+00:00",
"place_id": "556cd5dc4a75730453010000",
"type": "picture",
"updated_at": "2015-06-02T22:48:21.962Z",
"user_id": "556e26844a75730453170000"
},
{
"_id": "556e351f4a75730453280000",
"created_at": "2015-06-02T22:58:39.761Z",
"media_content_type": null,
"media_file_name": null,
"media_file_size": null,
"media_fingerprint": null,
"media_updated_at": null,
"picture_content_type": "image/jpeg",
"picture_file_name": "8580243774_479d5fe7bf_z.jpg",
"picture_file_size": 191938,
"picture_fingerprint": "61bdc1d21158c76d601b028bf826b437",
"picture_updated_at": "2015-06-02T22:58:39.571+00:00",
"place_id": "556cd5dc4a75730453010000",
"type": "picture",
"updated_at": "2015-06-02T22:58:39.761Z",
"user_id": "556e26844a75730453170000"
}
],
"telephone": "5555555555",
"thumb_picture_url": "/pictures/thumbnail/missing.png",
"updated_at": "2015-06-02T21:56:20.705Z",
"username": "tester1"
},
{
"_id": "556cd5934a75730453000000",
"authentication_token": "RErefcuH5eDsSaNw6gCB",
"created_at": "2015-06-01T21:58:43.198Z",
"dob": "1982-08-01",
"email": "test#gmail.com",
"gender": "Male",
"last_action": null,
"name": "tester",
"picture_content_type": null,
"picture_file_name": null,
"picture_file_size": null,
"picture_fingerprint": null,
"picture_updated_at": null,
"picture_url": "/pictures/original/missing.png",
"telephone": "5555555555",
"thumb_picture_url": "/pictures/thumbnail/missing.png",
"updated_at": "2015-06-01T21:58:43.200Z",
"username": "tester"
}
]
Your issue is not with each. It's either with Place.find or with resource[:posts]
Try to replace
post[:place] = Place.find(post[:place])
with
post[:place] = Place.find(post[:place_id])
--- edit ----
replace
resource[:posts].each do |post|
post[:place] = Place.find(post[:place])
end
with
resource[:posts].map! do |post|
post[:place_id] = Place.find(post[:place_id])
post
end
or
resource[:posts].each.with_index do |post, i|
resource[:posts][i][:place] = Place.find(post[:place_id])
end
or
resource.posts.each do |post|
post[:place] = Place.find(post.place)
end

wrong child root name in rabl and can't set child root name

I have to following rabl code to generate some JSON data.
object #event
attributes :id, :EID, :name, :address, :description, :latitude, :longitude, :time, :created_at
node(:rsvp_count) { |event| event.rsvp_users.count }
node(:check_in_count) { |event| event.checkedin_users.count }
node(:FID) { |event| event.creater.FID if event.creater}
child :rsvp_users, :object_root => false do
extends 'users/index'
end
child :checkedin_users, :object_root => false do
extends 'users/index'
end
And the data it generates looks like this:
[
{
"event": {
"id": 2,
"EID": 123458,
"name": "event no.2",
"address": "189 elm st",
"description": "awesome event",
"latitude": 10,
"longitude": 10,
"time": "2013-10-20T18:00:00Z",
"created_at": "2013-08-15T21:06:21Z",
"rsvp_count": 3,
"check_in_count": 0,
"FID": 12345678,
"users": [
{
"id": 4,
"FID": 112233445,
"name": "name1",
"using_app": true
},
{
"id": 3,
"FID": 9999,
"name": "name2",
"using_app": false
},
{
"id": 2,
"FID": 123456789,
"name": "name3-robot",
"using_app": true
}
],
"checkedin_users": []
}
}
]
You can ignore the event hash, the weird stuff is happening at the bottom in the 2 users array.
So as you can see, the child rsvp_users array is showing up with the name users even if I set the root param to "rsvp_users". However, for checkedin_users array (which is empty right now), I don't need to do anything, and it's name is automatically checkedin_users. What is happening here? Is it a bug in rabl? Or is it something that I am missing?
I've encountered the same exact bug, the problem seems to be setting the object_root to false.
Following the comment of Bigxiang I have experimented a bit and found that this works fantastically:
child( {:rsvp => :rsvp}, {:object_root => false} ) do
extends "users/index"
end
Note both the round parentheses "()" and braces "{}".

Creating a deeply nested object in Rails

I've built a small API that, when posted a JSON object, creates the representative model records. The data looks like this:
{
"customer": {
"email": "michael#myemail.com",
"first_name": "Michael T. Smith",
"last_name": "",
"shipping_address_1": "",
"telephone": "5551211212",
"source": "Purchase"
},
"order": {
"system_order_id": "1070",
"shipping_address_1": "",
"billing_address_1": "123 Your Street",
"shipping": "0",
"tax": "0",
"total": "299",
"invoice_date": 1321157461,
"status": "PROCESSING",
"additional_specs": "This is my info!",
"line_items": [
{
"quantity": "1",
"price": "239",
"product": "Thing A",
"comments": "comments"
"specification": {
"width": "12",
"length": "12",
},
},
{
"quantity": "1",
"price": "239",
"product": "Thing A",
"comments": "comments"
"specification": {
"width": "12",
"length": "12",
},
},
]
}
}
The question is how to create the nested objects. My models are setup as such:
class Order < ActiveRecord::Base
has_many :line_items
belongs_to :customer
accepts_nested_attributes_for :line_items
end
class LineItem < ActiveRecord::Base
belongs_to :order
has_many :specifications
end
class Specification < ActiveRecord::Base
belongs_to :LineItem
end
I'm trying to create the records using this code:
#order = #customer.orders.build(#data[:order])
#order.save
Is there a better way to do this? Currently I'm getting this error: ActiveRecord::AssociationTypeMismatch in ApiController#purchase_request LineItem(#70310607516240) expected, got Hash(#70310854628220)
Thanks!
accepts_nested_attributes_for defines a new setter method for the association: the original name with _attributes appended to it.
In your case, there is a line_items_attributes= method on your Order model, which is what you need to use to take advantage of the nested attributes feature. Something as simple as swapping the key before building the model would probably work, e.g.:
#data[:order][:line_items_attributes] = #data[:order].delete(:line_items)
#order = #customer.orders.build(#data[:order])
#order.save
You can use accepts_nested_attributes_for like this:
(in my example, Products has_many ProductionItineraries and ProductionItineraries belongs_to Products)
model/product.rb
has_many :production_itineraries, dependent: :delete_all
accepts_nested_attributes_for :production_itineraries, allow_destroy: true
model/production_itinerary.rb
belongs_to :product
To instantiate:
products = Product.new
products.production_itineraries.build(other_production_itineraries_fields)
Doing this, after save products, the production_itinerary object will be save automatically, with the respective product_id field

Resources