-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Description
I'm using version 0.10.2 and attempting to create a relationship (belongs_to
) with ActiveModelSerializers::Model
(and not ActiveRecord::Model
). The relationship output keeps returning null.
Thanks!
Model
class User::Registration < ActiveModelSerializers::Model
attr_reader :user
attr_accessor :email
def save
register_user
end
def email=(value)
# Follow Devise's configuration to check if email is case insensitive
@email = Devise.case_insensitive_keys.include?(:email) ? value.try(:downcase) : value
end
def id
email
end
private
def register_user
@user = User.new(email: email)
@user.provider = 'email'
@user.send_confirmation_instructions if @user.save
@user.clean_up_passwords
@user.persisted?
end
end
Resource
class User::RegistrationSerializer < ApplicationSerializer
attributes :email
belongs_to :user
end
Controller
def create
@registration = User::Registration.new(email: @request[:email])
@registration.save
render json: @registration, status: :created
end
Ouptut
{"data": {"id":"[email protected]","type":"user-registrations","attributes":{"email":"[email protected]"},"relationships":{"user":{"data":null}}}}
I'd expect the relationships output to not be null.
Metadata
Metadata
Assignees
Labels
No labels