Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Has_many method 'xxx_ids' #35

Open
BorisBresciani opened this issue Nov 21, 2018 · 4 comments
Open

Has_many method 'xxx_ids' #35

BorisBresciani opened this issue Nov 21, 2018 · 4 comments

Comments

@BorisBresciani
Copy link

BorisBresciani commented Nov 21, 2018

Hello,

Variables of type 'xxx_ids' (has_many) built automatically by ActiveRecord do not work in attributes mode

Exemple:

This example works

Model:

has_many :pankos

Serializer:

attributes :panko_ids
def panko_ids
  object.panko_ids
end

###############

This example does not work

Model:

has_many :pankos

Serializer:

attributes :panko_ids #return always null

However the variable panko_ids is well regarded as an attribute because I can do record.panko_ids or record.send(:panko_ids)

Ruby: 2.5.1
Rails: 5.2.1

Thx :)

@yosiat
Copy link
Owner

yosiat commented Nov 25, 2018

@BorisBresciani I can add support for has_many :pankos, embed: :ids in serializer like AMS.

wdyt?

@BorisBresciani
Copy link
Author

BorisBresciani commented Nov 25, 2018

It is a good idea or if not to do as simple attributes without specifying "has_many" like my example

@yosiat
Copy link
Owner

yosiat commented Nov 25, 2018

It's simple to do embed because at serializer definition time I don't know which model is serialized in advance so I can't tell if panko_ids is field or method and detecting this at runtime can be a huge performance loss.

I have two options to tackle this:

  • Move to strict serializes and don't allow polymorphic serializers, so in addition to attributes, you will specify model Post - and I can tell automatically which method it is, gather type information etc.
  • At runtime detect which model I am serializing and cache results of attributes mapping per serializer.

Both solutions will take some time to develop if you can wait two weeks (at max), I will implement them now and in the meanwhile you will do:

attributes :panko_ids
def panko_ids
  object.panko_ids
end

or

attributes :panko_ids
delegate :panko_ids to: :object

@BorisBresciani
Copy link
Author

Hi,

The second solution seems better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants