Error with associations using Active Model Serializers - active-model-serializers

When rendering the json of a model with nested associations I get the following error:
undefined method `serializable_hash' for #<ActiveModel::ArraySerializer:0x007fe761592d88>
This is my code:
class EventSerializer < ActiveModel::Serializer
attributes :id, :name, :date
has_many :markets
end
class MarketSerializer < ActiveModel::Serializer
attributes :id, :bet_limit_time
has_many :options
end
class OptionSerializer < ActiveModel::Serializer
attributes :id, :name, :odds
end
The error appears when rendering the options association.
It's a pretty simple case, but I have found no issue about it.

I think it's a problem with having an attribute named options. See https://stackoverflow.com/a/16005874/157943:
"It turns out that using :option as a property name was causing conflicts. There are probably other conflicting names as well. Gotta be careful about the namespace!"
Not sure how to fix it other than renaming the options association (possibly you could build it manually by overriding #attributes and constructing the hash manually?)

Related

UML relationship between the same class

I have an 'Account' class and (every) Account (instance) can have 0 or more guestAccounts (from the same 'Account' class).
I'm stuck of how to model this:
- should I model this as a unary association (recursive)?
Thanks for helping.
Yes indeed you can.
I am not sure that's solved your question, but you can do something like:
I define private for attributes visibility, public or default is often an error.

ActiveModel Serializer JSONAPI included resource

I am working with ActiveModel Serializer's JSONAPI adapter and I'm trying to include a "user" resource when I serialize a "video" resource. Currently my video serializer looks something like this:
class VideoSerializer < ActiveModel::Serializer
attributes :id, :uploaded_at, :title, :description
belongs_to :user
has_many :comments
included :user
end
I've spent some time looking through the recently closed issues here: https://github.com/rails-api/active_model_serializers/issues and it looks like this feature should be complete in the latest release I just can't seem to get it to work. Does anyone see what I might be doing wrong?
There is no included method defined in the serializer DSL. There is, though, an included adapter option, that allows one to specify which related resources should be included in the response document.
In your case (in your controller):
render json: videos, adapter: :json_api, include: 'user'

acts_as_tree and active_model_serializer

Currently I'm building an API where I have a model functioning as a category. The category has possible subcategories and/or a single parent category created using the acts_as_tree gem. I wish to serialize the category model and it's relations to itself using active_model_serializers gem.
Note: active_model_serializer uses the :json_api adapter
class NutritionCategory < ActiveRecord::Base
has_many :nutritions
acts_as_tree
end
class NutritionCategorySerializer < ActiveModel::Serializer
attributes :id, :name, :description
has_many :nutritions, embed: :ids
end
Since acts_as_tree does all the 'magic' I can't seem to find a way to serialize this relationship properly. How do I define the relationship in the serializer?
I'm using Ruby 2.2.1 with the following gems:
Rails (4.2.1)
acts_as_tree (2.1.0)
active_model_serializers (0.10.0.pre)
Try adding to your serializer:
has_many :children, embed: :ids

Type field of Property not filled in for UML PrimitiveType in Xpand

I have an XPT template that generates text from a UML model. This is the excerpt I use on the attributes of a class
«FOREACH attribute AS a»
Id: «a.name»
Type: «a.type.name»
«ENDFOREACH»
All works well as long as the type is a class from the model itself. But if it is a primitive type, then all of the fields, including the name, is set to null. If I change «a.type.name» to «a.type», then the response is something like this:
org.eclipse.uml2.uml.internal.impl.PrimitiveTypeImpl#6e315086 (eProxyURI:
pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#String)
The debugger shows that only the eStorage private field is set on the object that is why the toString() produces the output above.
Interestingly the same expression in Acceleo is evaluated correctly:
[query public getType(t : Type) : String = t.name /]
Question: how can I get the type field of attributes be filled in for primitive uml types in Xtend?
EDIT: Issue narrowed down to this question: EProxy URI does not resolve in ecore model
The code generated by Acceleo can resolve the unusual pathmap:// eProxyURIs. By default, a simple Xpand template or Xtend code can't resolve them. See this question for more details and the solution.

Difference between association and dependency?

In a UML class diagram, what is the difference between an association relationship and a dependency relationship?
From what I know, an association is a stronger relationship than a dependency, but I'm not sure how it is stronger.
Any example would be more than welcome :)
An association almost always implies that one object has the other object as a field/property/attribute (terminology differs).
A dependency typically (but not always) implies that an object accepts another object as a method parameter, instantiates, or uses another object. A dependency is very much implied by an association.
In OOP terms:
Association --> A has-a C object (as a member variable)
Dependency --> A references B (as a method parameter or return type)
public class A {
private C c;
public void myMethod(B b) {
b.callMethod();
}
}
There is also a more detailed answer.
What is the difference between dependency and association?:
In general, you use an association to represent something like a field
in a class. The link is always there, in that you can always ask an
order for its customer. It need not actually be a field, if you are
modeling from a more interface perspective, it can just indicate the
presence of a method that will return the order's customer.
To quote from the 3rd edition of UML Distilled (now just out) "a
dependency exists between two elements if changes to the definition of
one element (the supplier) may cause changes to the other (the
client)". This is a very vague and general relationship, which is why
the UML has a host of stereotypes for different forms of dependency.
In code terms, such things as naming a parameter type and creating an
object in a temporary variable imply a dependency.
...
Dependency is like when you define a method that takes a String(in Java, C#, as string is a object in them) as a parameter, then your class is dependent on String class.
Association is like when you declare a string as an attribute in your class.
then your code is associated with the string class.
String name = null //: is a association.
Dependency - A change in a class affects the change in it's dependent class. Example- Circle is dependent on Shape (an interface). If you change Shape , it affects Circle too. So, Circle has a dependency on Shape.
Association- means there is a certain relationship between 2 objects
(one-one, one-many,many-many)
Association is of 2 types-
Composition
Aggregation
1) Composition- stronger Association or relationship between 2 objects. You are creating an object of a class B inside another class A
public class A {
B b;
public void setB(){
this.b= new B();
}
}
If we delete class A , B won't exist( B object is created inside A only).
Another example -Body & Liver .Liver can't exist outside Body.
2) Aggregation - weaker type of Association between 2 objects.
public class A {
B b;
public void setB(B b_ref){
this.b= b_ref;
/* object B is passed as an argument of a method */
}
}
Even if you delete class A, B will exist outside(B is created outside and passed to Class A)
Another example of this- Man & Car . Man has a Car but Man & Car exist independently.
Here: "Association vs. Dependency vs. Aggregation vs. Composition", you have a great vade mecum with uml class diagrams and code snippets.
The author gives us a list of relationships: Association, Dependency, Aggregation, Composition in one place.
A dependency is very general and lowering complexity is about diminishing dependencies as much as possible.
An association is a strong (static) dependency. Aggregation and Composition are even stronger.
I was always checking this answer as it didn't stick in my mind. I found this one more helpful after reading the accepted answer
Association is when one object just has a link to another and don't use relational object methods. For ruby for example
class User
has_one :profile
end
user = User.first
profile = user.profile
profile.sign_out
It means you can get a profile object from user but user don't use profile's methods inside himself(has no dependency on a Profile's interface).
Dependency means that User has link to another object and call that object's methods inside himself
class User
has_one :profile
def personal_info
profile.info
end
end
Here if Profile's info method will be changed or renamed our Dependent User class also need to be changed.

Resources