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

POST url's incorrect #241

Open
poliphox opened this issue May 10, 2014 · 3 comments
Open

POST url's incorrect #241

poliphox opened this issue May 10, 2014 · 3 comments

Comments

@poliphox
Copy link

I have a couple Her::Model's User and Book's. A user has_many :books, and I can retrieve them with User.find(1).books => [Book, Book....]
However, User.find(1).books.create(title: "test book") sends a GET request to /users

This is pretty much the same as the example code, so I dont think I'm doing anything wrong in my code. Is there a known issue?

Many thanks!

@poliphox
Copy link
Author

Hmm, I've managed (somehow) to get the POST going to the correct URL, but now there's an issue when GET'ing the next resource.

I have this URL: /users/1/books/1/chapters
I can fetch Users and Books, but fetching Chapters doesn't work.

My User has_many :books, and a Book has_many :chapters (book belongs_to :user, and chapter belongs_to :book in my Her models).
However, calling :User.find(1).books.find(1).chapters sends a GET request to
/books/1/chapters (the /user part of the url is missing).

If I look at the object returned by User.find(1).books, the @parent variable is set to 'users/1'. For User.find(1).books.find(1).chapters, the @parent variable is set to 'chapters/1'. That seems correct to me, but I would assume the subsequent call to fetch the chapters would join these parent paths together.

My Her models are:

class User
    include Her::Model
    has_many :books
end
class Book
    include Her::Model
    belongs_to :user
    has_many  :chapters
end
class Chapter
    include Her::Model
    belongs_to :book
end

On the API side, each of the resources, apart from User, has the foreign_key for its parent resource (ie /users/1/books/1 has user_id, /users/1/books/1/chapters/1 has book_id).

Can resources be nested deeply, or is only 1 level supported?

thanks.

@poliphox
Copy link
Author

Replying to myself again :)
I've added collection_path and resource_paths to my nested models ("users/:user_id/books/:book_id/chapters" and "users/:user_id/books/:book_id/chapters/:chapter_id").

This is now returning data from the resources, however calling:

User.find(1).books.find(1).chapters.find(1) 

returns an object like Chapter(< unknown path, missing user_id >.
Obviously, I'm doing something wrong somewhere, I don't know why the Chapter is warning about the user_id, but not the book_id? Shouldn't it just be getting that from the parent model(s) anyway?

I'm trying to figure out why the Book->Chapter relationship is causing problems (unlike the User->Book one) but I can't find anything wrong in my code. Is this a bug in Her?

@ghost
Copy link

ghost commented May 16, 2014

I believe this is a duplicate of

#224

I put together a PR

#238

you are welcome to comment on it.

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

1 participant