当其中一个模型总是依赖另一个时,我应该有两个控制器吗?

时间:2021-02-15 14:13:37

I have two models User and Project with a one-to-many association.

我有两个模型User和Project具有一对多关联。

An instance (or collection) of Project is never "CRUD:ed" by itself, it's always in the context as a property of a @user.

Project的实例(或集合)本身从不“CRUD:ed”,它始终作为@user的属性在上下文中。

Should Project still have it's own Controller whose every action has to look up which User the request is for OR should I add project-related actions in the UsersController such as:

Project是否仍然拥有自己的Controller,其每个操作都必须查找请求所针对的用户,或者我应该在UsersController中添加与项目相关的操作,例如:

class UsersController < ApplicationController
  def show_projects
    @user.projects
  end
end

1 个解决方案

#1


1  

I will go for having also a controller for the Project models. If you think in RESTful actions then you can think in a call for something like /projects/1 (or other action with an url appending /user like /user/{ID_user}/projects/1) to get project with ID 1 and in the action to process that request checking user permission stuff. Also for example asking for /projects will process a call to get all user's project.

我将为项目模型提供一个控制器。如果您在RESTful操作中进行思考,那么您可以在调用/ projects / 1(或其他具有url追加/用户/ user / {ID_user} / projects / 1的操作)中调用ID为1的项目处理该请求检查用户权限的操作。例如,询问/ projects将处理调用以获取所有用户的项目。

I think that in spite of your Project model having a strong dependency with your User model, doesn't mean that you can't CRUD your Project model. I suppose that somewhere in your app you are going to need to create Projects and that's going to be the place to put CRUD actions for the Project model. Maybe i'm wrong about this (having little information about your app it's difficult to analyse further your problem) but your Project model sounds like a first class entity just like your User model.

我认为,尽管您的项目模型与您的用户模型有很强的依赖关系,但这并不意味着您无法对您的项目模型进行CRUD。我想在你的应用程序的某个地方,你需要创建项目,这将是为项目模型放置CRUD操作的地方。也许我错了(关于你的应用程序的信息很少,很难进一步分析你的问题),但你的项目模型听起来就像你的用户模型一样的第一类实体。

#1


1  

I will go for having also a controller for the Project models. If you think in RESTful actions then you can think in a call for something like /projects/1 (or other action with an url appending /user like /user/{ID_user}/projects/1) to get project with ID 1 and in the action to process that request checking user permission stuff. Also for example asking for /projects will process a call to get all user's project.

我将为项目模型提供一个控制器。如果您在RESTful操作中进行思考,那么您可以在调用/ projects / 1(或其他具有url追加/用户/ user / {ID_user} / projects / 1的操作)中调用ID为1的项目处理该请求检查用户权限的操作。例如,询问/ projects将处理调用以获取所有用户的项目。

I think that in spite of your Project model having a strong dependency with your User model, doesn't mean that you can't CRUD your Project model. I suppose that somewhere in your app you are going to need to create Projects and that's going to be the place to put CRUD actions for the Project model. Maybe i'm wrong about this (having little information about your app it's difficult to analyse further your problem) but your Project model sounds like a first class entity just like your User model.

我认为,尽管您的项目模型与您的用户模型有很强的依赖关系,但这并不意味着您无法对您的项目模型进行CRUD。我想在你的应用程序的某个地方,你需要创建项目,这将是为项目模型放置CRUD操作的地方。也许我错了(关于你的应用程序的信息很少,很难进一步分析你的问题),但你的项目模型听起来就像你的用户模型一样的第一类实体。