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. Here's the problem:
When the current_user is an employer, I'd load the employments with the user associations, which works fine. When the current_user is not an employer, I want to load the employments belonging to the user.
As the EmploymentSerializer has_one :user, this results in a recursive query where the current_user has_one employment has_one current_user ad infinitum.
I've tried adding this method in the EmploymentSerializer but it doesn't work:
def include_user?
object.user != scope
end
How do I load the current_user's employments?
Solution
The recursion was caused by another serializer, and this question is irrelevant. AMS works as it should.