user2954587
user2954587

Reputation: 4871

Rails Polymorphic RESTful Routing

I have a polymorphic relationship between User, PatientProfile and DoctorProfile. I'm trying to create RESTful routes for Review. What is the best practice for this?

class User
 belongs_to :profile, polymorphic: true

class PatientProfile
 has_one :user, as: :profile
 has_many :reviews

class DoctorProfile
 has_one :user, as: :profile
 has_many :reviews

class Review
 belongs_to :patient_profile
 belongs_to :doctor_profile

One approach is to create distinct routes for both DoctorProfile and PatientProfile instead of using Users. The drawback to this approach is that you would need two reviews controllers DoctorProfileReviewsController and PatientProfileReviewsController:

resources :patient_profile
  resources :reviews
end
resources :doctor_profile
  resources :reviews
end

What is best way to structure a REST API given these models?

Upvotes: 0

Views: 188

Answers (1)

fnln
fnln

Reputation: 230

\doctors\:id\reviews
\doctors\:id\reviews\:id
\patients\:id\reviews
\patients\:id\reviews\:id
\reviews\:id

Each doctor should be able to have it's reviews queried, each patient should be able to have it's reviews queried, each review should be able to be queried.

Making every resource be accessible is good practice here, if each resource is an active part of the system you'll probably eventually use all of them.

Upvotes: 1

Related Questions