具有高级关系的 Rails 5 RESTful API

Ter*_*ndo 3 ruby routing ruby-on-rails ruby-on-rails-5

我有很多具有高级关系的资源(habtm/hm/hmt 等),你可以想象的一切,但现在是时候为这个 API 编写一个漂亮的路由了。问题是,我无法找到关于嵌套资源 + 高级关系的最佳实践来做我的路由,这是我想要做的:

这是我的模型与相关关系

# app/models/candidate.rb
class Candidate < ApplicationRecord
  include Sociable, Locatable

  belongs_to :user
  has_many :sourcing_accounts
  has_many :accounts, through: :sourcing_accounts
  has_many :users, through: :sourcing_accounts
end

# app/models/sourcing_account.rb
class SourcingAccount < ApplicationRecord
  belongs_to :account
  belongs_to :candidate
  belongs_to :user
end

# app/models/user.rb
class User < ApplicationRecord
  include Sociable

  has_many :candidates
  has_many :campaigns
  has_many :sourcing_account
end
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我愿意允许创建之间的关系CandidateUser通过创建SourcingAccount

resources :candidates do
  resources :accounts
  resources :users, only: [:index] do
    post :remove
    post :add
  end
end
Run Code Online (Sandbox Code Playgroud)

它生成:

v1_candidate_user_remove POST   /v1/candidates/:candidate_id/users/:user_id/remove(.:format) api/v1/users#remove {:subdomain=>"api", :format=>:json}
   v1_candidate_user_add POST   /v1/candidates/:candidate_id/users/:user_id/add(.:format)    api/v1/users#add {:subdomain=>"api", :format=>:json}
Run Code Online (Sandbox Code Playgroud)

我没有发现任何关于此的信息。有最佳实践吗???如果没有,您认为最适合这种情况的是什么?

没有精确性,Rails 想要将其路由到 users#remove 和 users#add,我认为这是完全错误的。这些操作不得属于用户控制器。

奖金:

创建Account属于 2 个其他模型的多态路线(带有存在验证)应该是什么样的? 2 个模型Source是多态的[Candidate,User] # for example,另一个是多态的(它们是Sociable模型)

max*_*max 5

最佳实践是永远不要*嵌套超过一层的资源,并且只在需要嵌套或提供上下文的地方嵌套。

请记住,任何具有唯一 id 或 uid 的记录都可以在没有上下文的情况下直接获取。因此,不必要地嵌套成员路由会使您的 API 过于复杂且非常冗长。

DELETE /as/:id
is a lot better than
DELETE /as/:a_id/bs/:b_id/c/:id # Are you kidding me!
Run Code Online (Sandbox Code Playgroud)

让我们以一个经典的微博应用程序为例:

class User
  has_many :posts, foreign_key: 'author_id'
  has_many :comments
end

class Post
  belongs_to :author, class_name: 'User'
end

class Comment
  belongs_to :user
  belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)

您可以将路线声明为:

resources :users do
  scope module: :users do
    resources :posts, only: [:index]
    resources :comments, only: [:index]
  end
end

resources :posts do
  resources :comments, module: :posts, only: [:index, :create]
end

resources :comments, only: [:index, :destroy, :update]
Run Code Online (Sandbox Code Playgroud)

使用 module 选项可以让我们在“基本资源”的控制器及其嵌套表示之间进行区分:

class API::V1::PostsController < ApplicationController
  # GET /api/v1/posts
  def index
    @posts = Post.all
  end

  def show
    # ...
  end
  def destroy
    # ...
  end
  def update
    # ...
  end
end

# Represents posts that belong to a user
class API::V1::Users::PostsController < ApplicationController
  # GET /api/v1/users/:user_id/posts
  def index
    @user = User.eager_load(:posts).find(params[:user_id])
    respond_with(@user.posts)
  end
end
Run Code Online (Sandbox Code Playgroud)

在某些情况下,如果资源应该在另一个上下文中创建,您还需要嵌套来嵌套创建操作:

class API::V1::Posts::CommentsController < ApplicationController
  # PATCH /api/v1/posts/:post_id/comments
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(comment_params)
    respond_with(@comment)
  end

  # GET /api/v1/posts/:post_id/comments
  def index
    @post = Post.eager_load(:comments).find(params[:post_id])
    respond_with(@post.comments)
  end
end
Run Code Online (Sandbox Code Playgroud)