如何定义端点?
How do I define my endpoint?
在我的 API 中,我希望端点提供主题帐户的 ID 并拥有与该主题关联的 return 其他帐户记录。我的模型如下:
这是我的主账户模型。 subject 和 associates 都是 account 类型:
class TabAccount < ApplicationRecord
has_many :subjects, through: :subject_tab_account_relationships, source: :subject
has_many :subject_tab_account_relationships, foreign_key: :id_associate, class_name: "TabAccount"
has_many :associates, through: :associate_tab_account_relationships, source: :associate
has_many :associate_tab_account_relationships, foreign_key: :id_subject, class_name: "TabAccount"
end
这是我的账户关系模型。这提供了主题和关联之间的多对多自连接关系:
class TabAccountRelation < ApplicationRecord
belongs_to :ref_relationship_type
belongs_to :subject, foreign_key: "id_subject", class_name: "TabAccount"
belongs_to :associate, foreign_key: "id_associate", class_name: "TabAccount"
end
我知道这行得通,因为我可以在我的 Rails 代码中使用该模型,但我不完全确定如何通过端点公开这种关系。我知道在路由映射文件中您应该将子资源添加到父资源,但我不确定在这种情况下子资源是什么。这是我目前的路线图:
resources :tab_accounts do
resources :associates
end
这是我的账户管理员:
# GET /tab_accounts/:id_subject/associates
# GET /tab_accounts/:id_subject/associates.json
def index
if params[:id_subject]
_limit = if params[:limit].present? then params[:limit] else 100 end
@tab_accounts = TabAccount.find(params[:id_subject]).associates.limit(_limit)
else
@tab_accounts = TabAccount.offset(params[:offset]).limit(_limit)
end
end
这是我对 stderr 的输出:
Started GET "/tab_accounts/2646/associates.json?limit=5" for 127.0.0.1 at 2018-03-21 22:51:54 -0400
ActionController::RoutingError (uninitialized constant AssociatesController):
有没有人以前通过端点暴露过 aliases/sources?你是怎么做到的?我快做对了吗?
您可以向现有资源添加额外的路由,而不是添加嵌套资源。参见 http://guides.rubyonrails.org/routing.html#adding-more-restful-actions。
所以不要使用这个:
resources :tab_accounts do
resources :associates
end
你可以切换到这个:
resources :tab_accounts do
member do
get :associates
end
end
然后您可以将相应的 associates
操作添加到现有的帐户控制器中。
在我的 API 中,我希望端点提供主题帐户的 ID 并拥有与该主题关联的 return 其他帐户记录。我的模型如下:
这是我的主账户模型。 subject 和 associates 都是 account 类型:
class TabAccount < ApplicationRecord
has_many :subjects, through: :subject_tab_account_relationships, source: :subject
has_many :subject_tab_account_relationships, foreign_key: :id_associate, class_name: "TabAccount"
has_many :associates, through: :associate_tab_account_relationships, source: :associate
has_many :associate_tab_account_relationships, foreign_key: :id_subject, class_name: "TabAccount"
end
这是我的账户关系模型。这提供了主题和关联之间的多对多自连接关系:
class TabAccountRelation < ApplicationRecord
belongs_to :ref_relationship_type
belongs_to :subject, foreign_key: "id_subject", class_name: "TabAccount"
belongs_to :associate, foreign_key: "id_associate", class_name: "TabAccount"
end
我知道这行得通,因为我可以在我的 Rails 代码中使用该模型,但我不完全确定如何通过端点公开这种关系。我知道在路由映射文件中您应该将子资源添加到父资源,但我不确定在这种情况下子资源是什么。这是我目前的路线图:
resources :tab_accounts do
resources :associates
end
这是我的账户管理员:
# GET /tab_accounts/:id_subject/associates
# GET /tab_accounts/:id_subject/associates.json
def index
if params[:id_subject]
_limit = if params[:limit].present? then params[:limit] else 100 end
@tab_accounts = TabAccount.find(params[:id_subject]).associates.limit(_limit)
else
@tab_accounts = TabAccount.offset(params[:offset]).limit(_limit)
end
end
这是我对 stderr 的输出:
Started GET "/tab_accounts/2646/associates.json?limit=5" for 127.0.0.1 at 2018-03-21 22:51:54 -0400
ActionController::RoutingError (uninitialized constant AssociatesController):
有没有人以前通过端点暴露过 aliases/sources?你是怎么做到的?我快做对了吗?
您可以向现有资源添加额外的路由,而不是添加嵌套资源。参见 http://guides.rubyonrails.org/routing.html#adding-more-restful-actions。
所以不要使用这个:
resources :tab_accounts do
resources :associates
end
你可以切换到这个:
resources :tab_accounts do
member do
get :associates
end
end
然后您可以将相应的 associates
操作添加到现有的帐户控制器中。