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

Error with namespaced relations with inclusions #16

Closed
42atomys opened this issue Feb 1, 2018 · 3 comments
Closed

Error with namespaced relations with inclusions #16

42atomys opened this issue Feb 1, 2018 · 3 comments

Comments

@42atomys
Copy link

42atomys commented Feb 1, 2018

An error is perform when includes an associations has_one

class Cluster::LocationSerializer 
  include FastJsonapi::ObjectSerializer
  attributes :begin_at, :end_at

  has_one :workspace, serializer: Cluster::WorkspaceSerializer
  has_one :user, serializer: Cluster::UserSerializer
end
class Cluster::UserSerializer 
  include FastJsonapi::ObjectSerializer
  attributes :uid, :login, :nickname, :avatar_url
end
class Cluster::WorkspaceSerializer 
  include FastJsonapi::ObjectSerializer
  attributes :identifier, :campus_id
end

call:

active_locations = Location.active.includes(:workspace, :user).references(:workspace, :user)
Cluster::LocationSerializer.new(active_locations, include: [:workspace, :user]).serializable_hash

Error :

NameError - uninitialized constant Cluster::Cluster
@42atomys 42atomys changed the title Namespaced relations with inclussions Errorwith namespaced relations with inclusions Feb 1, 2018
@42atomys 42atomys changed the title Errorwith namespaced relations with inclusions Error with namespaced relations with inclusions Feb 1, 2018
@grossadamm
Copy link
Contributor

Interesting.. I'd like to build a test to replicate this failure so I can verify it's truly resolved.

@sriniwasgr-zz
Copy link

sriniwasgr-zz commented Feb 1, 2018

@AtomGrafiks i believe you have a typo in the Location Serializer. The optional key 'serializer' expects a symbol and by convention assumes all serializers are in the same namespace.

class Cluster::LocationSerializer 
  include FastJsonapi::ObjectSerializer
  attributes :begin_at, :end_at

  has_one :workspace, serializer: :workspace # (serializer hash not required in your case)
  has_one :user, serializer: :user  # (serializer hash not required in your case)
end

In your specific case, you dont even have to provide the key 'serializer' since your relationship name and serializer name are same.

has_one :workspace

@42atomys
Copy link
Author

42atomys commented Feb 2, 2018

Ah, interesting, thanks ;)

@42atomys 42atomys closed this as completed Feb 2, 2018
kpheasey pushed a commit to listrophy/fast_jsonapi that referenced this issue Oct 25, 2019
* Add params to set_id block arguments

Pull request Netflix#331 added a block to the ObjectSerializer.set_id class
method, which allows passing a block to the set_id method. Currently
this block takes only one argument `record`:

```
set_id do |record|
  "#{record.name.downcase}-#{record.id}"
end
```

This PR adds another argument `params` to the block:

```
set id do |record, params|
  params[:admin] ?  record.id : "#{record.name.downcase}-#{record.id}"
end
```

This customization can be useful in situation where we serve different
clients that may need different IDs. One nice side effect is also that
the `set_id` method has the same method signature as the `attribute`
method.

* Update the README
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

3 participants