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

Custom endpoint returns objects only of self class #106

Open
babrovka opened this issue Aug 11, 2015 · 3 comments
Open

Custom endpoint returns objects only of self class #106

babrovka opened this issue Aug 11, 2015 · 3 comments

Comments

@babrovka
Copy link

For example, we have two models:

User ans Post

Post belongs_to :user
User has_many :posts

I want to have custom endpoint in User model that returns his signed posts,
if I make something like this:

custom_endpoint :signed_posts, on: :member, request_method: :get

It goes to correct url:

/users/:id/signed_posts

but returns posts wrapped as User class objects

Is there any solutions for this?

@chingor13
Copy link
Collaborator

Currently, there is not a solution to do this with a custom endpoint.

If you fetch the user object and your server returns the linked path for signed posts (and you have a SignedPost resource), it should follow the link definition url and fetch SignedPost objects.

You could also have your SignedPost resource belongs_to :user and fetch with SignedPost.where(user_id: [userid]).all

@jsamos
Copy link

jsamos commented Apr 11, 2016

You can also write a custom parser that resolves the model from the "type"

def handle_data(result_set, data)
  ...
  record_class = choose_model_for(result_set, res)
  ...
end

def choose_model_for(result_set, res)

  res_type_name = res['type'].singularize.camelize
  (res_type_name.safe_constantize) ? res_type_name.safe_constantize : result_set.record_class

end

@JohnSmall
Copy link

This answer looks like a candidate for a pull request. Any time we get the data it should always infer the class from the returned type.

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

4 participants