Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails中使用has_many to:查询当前和以前的关系_Ruby On Rails_Ruby On Rails 4_Activerecord_Has Many Through_Has Many - Fatal编程技术网

Ruby on rails 在Rails中使用has_many to:查询当前和以前的关系

Ruby on rails 在Rails中使用has_many to:查询当前和以前的关系,ruby-on-rails,ruby-on-rails-4,activerecord,has-many-through,has-many,Ruby On Rails,Ruby On Rails 4,Activerecord,Has Many Through,Has Many,我有一个项目管理应用程序,它有3个模型…项目、用户和参与。我希望能够显示一个列表,其中列出了与给定用户在给定用户参与的任何项目上进行协作的所有用户。这包括过去的项目 例如,UserA和UserB在ProjectBlue中 用户A和用户C均为黄色 UserA&UserC&UserD在ProjectRed中 如果我在UserA的仪表板中,我希望能够看到一个列表,上面写着: “您曾与以下人员合作: UserC(仅列出一次) 用户 用户B“ 我是否需要为每个共享连接创建一个带有:user\u id、:p

我有一个项目管理应用程序,它有3个模型…项目、用户和参与。我希望能够显示一个列表,其中列出了与给定用户在给定用户参与的任何项目上进行协作的所有用户。这包括过去的项目

例如,UserA和UserB在ProjectBlue中 用户A和用户C均为黄色 UserA&UserC&UserD在ProjectRed中

如果我在UserA的仪表板中,我希望能够看到一个列表,上面写着:

“您曾与以下人员合作: UserC(仅列出一次) 用户 用户B“

我是否需要为每个共享连接创建一个带有:user\u id、:project\u id、记录的完整:relationships表?这似乎效率不高

型号

Class Project < ActiveRecord::Base
  belongs_to :user # (this is the organizer)
  has_many :users, through: :participants
  has_many :participants

Class User < ActiveRecord::Base 
  has_many :projects
  has_many :project, through :participant
  has_many :participants

Class Participant < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
Class项目
这是正确的方法。数据库空间是免费的,而且数据库很无聊。它会很感激使用一些数据。;-)

请看一些例子

但我不确定你的模型。它们看起来确实有点奇怪。你可能想要:

Class Project < ActiveRecord::Base
  belongs_to :user # (this is the organizer)
  has_many :participants
  has_many :users, through: :participants

Class User < ActiveRecord::Base 
  has_many :participants
  has_many :projects, through: :participants

Class Participant < ActiveRecord::Base
  belongs_to :project
  belongs_to :user
Class项目
谢谢。我是否应该删除:user\u id(作为组织者)从项目中编制索引并只将组织者关系存储在参与者中?一个项目总是一个所有者吗?或者一个项目可以有多个所有者吗?一个项目有一个创建者,但多个创建者可以更新它。我只想记录是谁创建的。例如,助手可以实际创建项目,但经理可以“拥有”“它。用于记录您想要查看的所有更改