active-model-serializers

Error sideloading JSON for act-as-taggable tags using active-model-serializers

被刻印的时光 ゝ 提交于 2019-12-04 22:01:08
问题 In a Ruby 2/Rails 4 app, I am trying to use acts-as-taggable-on in conjunction with active_model_serializers in order to create a JSON API that would output my tags along with other model parameters. First some background/motivation for this issue: The JSON is being fed into ember/ember-data, which as of the time of this writing has removed support for embedded records in JSON. There is a supposed fix in the documentation about this, but I find it clumsy and also haven't actually gotten it to

Side-loading objects with non-standard class names in EmberJS with Rails+active_model_serializers

a 夏天 提交于 2019-12-04 11:46:40
问题 I have a few models in Rails that look something like this: class Issue < ActiveRecord::Base belongs_to :reporter, class_name: 'User' belongs_to :assignee, class_name: 'User' has_many :comments end class User < ActiveRecord::Base end class Comment < ActiveRecord::Base end with serializers like so: class IssueSerializer < ActiveModel::Serializer attributes :id embed :ids, include: true has_one :reporter, :embed => :ids has_one :assignee, :embed => :ids end class UserSerializer < ActiveModel:

How to select serializer for nested objects with active_model_serializers

本秂侑毒 提交于 2019-12-04 08:26:46
I am using rails 4.0.0 and am looking for a way to serialize a custom object which contains predefined objects with these predefined object's serializers. Example: I have a model Student with a serializer StudentSerializer. I want to render a JSON object as follows: { user_type: "student" student: { id: 1 email: fake@email.com } } My serializer written for Student only serializes the email and id attributes. However when I call: render json: {user_type="student", student: stu} I get the full object of student back with all attributes. Is it possible to pick a serializer with active_model

How to perform eager loading in active_model_serializers

天涯浪子 提交于 2019-12-03 21:43:26
I have a model with several layers of nested associations. e.g., ModelA has_many: model_b ModelB has_one : model_c ModelC has_many: model_d ModelD has_many: model_e ... In serializers, embed :ids, include: true is used for sideloading: class ModelASerializer < ActiveModel::Serializer embed :ids, include: true has_many: model_b attributes: ... end class ModelBSerializer < ActiveModel::Serializer embed :ids, include: true ... When model_a is rendered, it has a serious "n+1 problem" and will generate thousands of calls like: ModelC Load (0.3ms) SELECT "model_cs".* FROM "model_cs" WHERE "model_cs"

Serialising async hasMany relationships

社会主义新天地 提交于 2019-12-03 20:16:00
问题 I'm fairly new to EmberJS, and I've been fiddling around for most of the evening trying to persist some hasMany and many to many relationships in a new app that I'm working on. I'm using Ember Data with ActiveModelAdapter to hook it up to my rails backend which is using ActiveModelSerializers I'm using the following versions of Ember and Ember Data that I'm using. DEBUG: ------------------------------- DEBUG: Ember : 1.6.0-beta.1+canary.d0f5f254 DEBUG: Ember Data : 1.0.0-beta.7+canary

Error sideloading JSON for act-as-taggable tags using active-model-serializers

无人久伴 提交于 2019-12-03 13:37:45
In a Ruby 2/Rails 4 app, I am trying to use acts-as-taggable-on in conjunction with active_model_serializers in order to create a JSON API that would output my tags along with other model parameters. First some background/motivation for this issue: The JSON is being fed into ember/ember-data, which as of the time of this writing has removed support for embedded records in JSON. There is a supposed fix in the documentation about this, but I find it clumsy and also haven't actually gotten it to work. Since I'm an Ember newbie and am a little more comfortable with Rails, I figure I'll try to

Use ActiveModel::Serializers to include two parent json arrays

自古美人都是妖i 提交于 2019-12-03 09:10:52
问题 I'm trying to send my front-end application json that looks like this: { facilities: [ {id: 5, name: 'happy days ranch', location: { address: '1424 Pastoral Lane', zipcode: '25245'}, instructor_ids: [2, 4, 9]} ], instructors: [ {id: 4, name: 'Johnny Pheonix', skill: '8', picture: 'aws_url', facility_ids: [5, 8, 12} ] } Things I have tried render :json => @facilities The serializer discovers this. Yay! But this does not include any instructors render :json => {facilities: @facilities,

Serializing the errors hash in ActiveModel::Serializer

大憨熊 提交于 2019-12-03 07:17:47
I'm using ActiveModel::Serializer to customize the JSON responses for my API. This works fine in most cases, except when it fails to save a model successfully. For example, def create def create book = Book.new(book_params) book.save respond_with book, location: nil end end As I understand it, the respond_with action will basically execute code that looks something like this (in order to generate the response). if resource.errors.any? render json: {:status => 'failed', :errors => resource.errors} else render json: {:status => 'created', :object => resource} end This does match up with what I'm

Serializing deeply nested associations with active_model_serializers

若如初见. 提交于 2019-12-03 04:44:20
问题 I'm using Rails 4.2.1 and active_model_serializers 0.10.0.rc2 I'm new to API's and chose active_model_serializers because it seems to be becoming the standard for rails (Although I'm not opposed to using RABL or another serializer) The problem I'm having is that I can't seem to include various attributes in multi-level relationships. For instance I have: Projects class ProjectSerializer < ActiveModel::Serializer attributes :id, :name, :updated_at has_many :estimates, include_nested

Rails Active Model Serializer - has_many and accessing the parent record

折月煮酒 提交于 2019-12-03 04:43:51
问题 I'm trying to build a JSON representation of some Rails models using Active Model Serializer, where some models embed others. For example, I have Event and Attendees, Event has_and_belongs_to_many Attendees. class EventSerializer < ActiveModel::Serializer attributes :name has_many :attendees, serializer: AttendeeSerializer end class AttendeeSerializer < ActiveModel::Serializer attributes :name end This would result in JSON like { name: 'Event One', attendees: [{ name: 'Alice' }, { name: 'Bob'