FAQ Database Discussion Community


ActiveModel serializer inheritance

ruby-on-rails,active-model-serializers
say I have this serializer class FooSerializer < ActiveModel::Serializer attributes :this, :that, :the_other def this SomeThing.expensive(this) end def that SomeThing.expensive(that) end def the_other SomeThing.expensive(the_other) end end Where the operations for the individual serialized values is somewhat expensive... And then I have another serializer that whats to make use of that,...

ruby active model serializer association on version > 0.8.0

ruby-on-rails,ruby,active-model-serializers
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 "active_model_serializers", "~> 0.8.0" but, doesn't work with gem 'active_model_serializers', git: "https://github.com/rails-api/active_model_serializers.git" ...

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

ruby-on-rails-4,active-model-serializers
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 address". Model class Lender < ActiveRecord::Base ......

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

javascript,ruby-on-rails-4,kendo-grid,link-to,active-model-serializers
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", "https://www.google.com/search?q=#{company.name}%20leadership%20team") %> I don't need to store this link in the database, so...

How to apply ActiveModel::Serializer to a custom object?

ruby-on-rails,json,ruby-on-rails-4,activemodel,active-model-serializers
If I specify simple array of ActiveModel objects serializer works: format.json { render json: @childs, each_serializer: ItemSerializer } But I need to respond with JSON with additional fields, such as parent_id, etc. {parent_id: 15, childs: @childs} Any idea how to achieve it? item_serializer.rb class ItemSerializer < ActiveModel::Serializer attributes :id, :name,...

Only serialize a single object with ActiveModel::Serializer

ruby-on-rails,serialization,active-model-serializers
In my project I have a model DrinkPayment: class DrinkPayment < ActiveRecord::Base #Association belongs_to :drink belongs_to :participation end And my Serializer for this model: class DrinkPaymentSerializer < ActiveModel::Serializer ActiveModel::Serializer.setup do |config| config.embed = :ids config.embed_in_root = true end attributes :id, :participation_id, :drink_id has_one :participation has_one :drink end Doing it like...

How to standardize JSON results of Rails project

ruby-on-rails,ruby,json,active-model-serializers
I need to add extra tags to JSON results of Rails project. GET /menus { meta: { code: 200, message: "" } data: [ // default rails response goes here ] } I don't want to do something like this in the controllers: render json: { meta: { code: 200,...

Rails 4.2 Active Record Serializer JSON format issue

ruby-on-rails,json,activerecord,active-model-serializers
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 UserFeedSerializer.new(object.user) end end end I expected the JSON response to be like { "feed": { "id": 10 "user": { "id": 10 }...

Ruby: How do I include methods from a Module into a subclass of ActiveModel::Serializer

ruby-on-rails,active-model-serializers,rspec3
I'm trying to include a method from a helper module into an ActiveModel::Serializer subclass but for some reason the method is not showing up. Here's my simple helper module: module Helpers module Serialisers def enforce_zulu_time(attribute) define_method(attribute) do object.send(attribute).utc.iso8601 unless object.try(attribute).nil? end end end end And here's my test serialiser class...

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

ruby-on-rails-3,ruby-on-rails-3.2,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| item.strip.to_sym} embed.each do |assoc| include! assoc if _associations.keys.include?(assoc) end end end end class EventSerializer < BaseSerializer attributes :id, :name has_many :organizers, serializer: OrganizerSerializer has_many :participants, serializer:...

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

json,ruby-on-rails-4,active-model-serializers
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 trickier part is that running each command through the command line still works fine. Here...

Active model serializer association unless object.user == current_user

ruby-on-rails,active-model-serializers,rails-api
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 attributes :id has_one :user class UserSerializer < ActiveModel::Serializer attributes :id Everything works as it should....

How to use a dynamic value for cache key with ActiveModel::Serializers (v0.10.0.rc1)

caching,active-model-serializers,rails-api
I am using a database-driven solution for labels and translations that I would like to cache at the serializer level. Here is my serializer. class AppLabelSerializer < ActiveModel::Serializer cache key: 'app_label', expires_in: 3.hours attributes :id, :key, :label, :label_plural def key object.app_label_dictionary.key end end The problem is that I need to...

Serializing a custom attribute

ruby-on-rails,json,serialization,active-model-serializers
I am using the Active Model Serializer gem for my application. Now I have this situation where a user can have an avatar which is just the ID of a medium. I have the avatar info saved into Redis. So currently what I do to show the avatar at all...

ActiveModel::Serializer not working

javascript,ruby-on-rails,active-model-serializers
I have a Client model and a method in my controller that should return the nearest clients. I'm using ActiveModel::Serializers but it's not working. class ClientSerializer < ActiveModel::Serializer attributes :id, :name, :path, :url def url client_url(object) end def path client_path(object) end end controller: def nearby_from_category @closest_clients = Client.from_category(params[:category]). activated.locateable.all_with_translation(@lang). by_distance(origin:...