Nested resources not sending data to db

52 views
Skip to first unread message

KnightCanti

unread,
Jan 11, 2023, 8:09:30 AM1/11/23
to Grape Framework Discussion
I am new to rails and grape so forgive the naivety of the question. 

I am trying to use nested resources to make CRUD requests to my postgres db. 

1. First off, when building an API through grape that has nested resources is the conventional structure, API::V1::Parent::Child, when mounting applications? I am not currently doing that, but rather having the parent and child be siblings in term of folder structure (addresses is a child of people)
image_2023-01-11_064646289.png

2. Secondly when calling the nested resources is it okay to say 
resources :parent do
         route_param :id do
                    params do 
                             { child attributes for the model being queried}
                     end
post route_url do
        if Parent.find(:id)
                 Child.create(declared(params))
Screenshot 2023-01-11 063719.jpg

3. If question 2 is okay, then why does the query not enter the data into the database? (I was able to enter data into the db using its own separate module) When I currently try with what I have, through postman, the response is the same body that I attempted to add to the db.
image_2023-01-11_065056605.png
Followed this guide to get started with grape

Daniel D.

unread,
Jan 12, 2023, 2:29:32 PM1/12/23
to ruby-...@googlegroups.com
1. Grape generally is not opinionated about nesting or parent/child relationships.
2. Seems ok.
3. I would examine the data and test .create(data) separately, I suspect the problem is that your database layer doesn't support this.

Now, stepping back, I think what you're proposing is a bad idea (TM). I gave a talk about this a few years ago, unfortunately the video is gone but slides are here. I recommend adopting a standard like Hypermedia. The canonical example of how you quickly create a confusing API with this model is on slide 5.

Hope this helps!

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-grape/12151d53-9bd1-49fa-9189-621f638a4c29n%40googlegroups.com.


--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - t:@dblockdotorg - ig:@artdblockdotorggithub/dblock

Reply all
Reply to author
Forward
0 new messages