I have two different models: User and Organization, orgs for short. I would like both to be able to create posts, so both have a one-to-many relationships with Post model.
I set my table as such:
create_table "posts", force: :cascade do |t|
t.text "content"
t.integer "likes"
t.integer "user_id"
t.integer "org_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
My routes:
resources :users do
resources :posts
end
resources :orgs do
resources :posts
resources :users
end
My Outcome
// http://localhost:3001/api/v1/**users**/4/posts
[
{
"id": 3,
"content": "This a user's post",
"likes": 0,
"user_id": 4,
"org_id": null,
},
{
"id": 5,
"content": "This is an org's post",
"likes": 0,
"user_id": null,
"org_id": 2,
}
]
I am able to post as a user and as an org, however when I route to either my user or org, it shows the post for both.
Is there a way to show only user's post when route to user and only org's post when route to orgs?