object
method inside an Alba::Resource
#319
-
I'm new to Alba and I come from AMS. While trying to migrate, I was trying to serialize a list of objects:
In my serializer I had: class SomeSerializer
include Alba::Resource # This was before an `ActiveModel::Serializer`
attribute :id do
object.token
end
end This was giving me an error: *** NoMethodError Exception: undefined method `token' for #<ActiveRecord::AssociationRelation [#<User... This is because object for I had to switch to attribute :id do |resource|
resource.token
end This will require substantial changes to the serializers. What's the background on the whole object being available to the serializer? I was thinking if I could define somehow an |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@davidgm0 Actually there IS # this `users` is `object` in serializer class
SomeSerializer.new(users).serialize Alba is not meant be 100% compatible with AMS, but we'd like to make migration as easy as possible. One simple way is to rename a block argument from # This should work
attribute :id do |object|
object.token
end Then it's easier to do so with automation such as editor macro, just adding |
Beta Was this translation helpful? Give feedback.
-
Good tip, thanks! |
Beta Was this translation helpful? Give feedback.
@davidgm0 Actually there IS
object
method, but it points to the object given to a resource class during initialization.Alba is not meant be 100% compatible with AMS, but we'd like to make migration as easy as possible.
In this case, if
object
isuser
instead ofusers
, it's better for migration.One simple way is to rename a block argument from
resource
toobject
.Then it's easier to do so with automation such as editor macro, just adding
|object|
to each end ofattribute
line.