Rails 有很多并且属于一个

发布于 2024-11-17 16:27:58 字数 1233 浏览 2 评论 0原文

我有一个 User 模型,其中有许多 projects 和一个 Project 模型,它可以有许多 user,但也属于单个用户(即创建该项目的用户)。它必须属于用户。它还允许将用户列表与之关联,即协作。

考虑到这一点,我的模型看起来像这样:

class User < ActiveRecord::Base
  has_many :assigned_projects
  has_many :projects, :through => :assigned_projects
end

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :assigned_projects
  has_many :users, :through => :assigned_projects
end

class AssignedProject < ActiveRecord::Base
  belongs_to :user
  belongs_to :project
end

现在,当我想通过 User 创建一个新项目时,我会这样做:

user = User.create(:name => 'injekt')
user.projects.create(:name => 'project one')

现在,我知道 projects< /code> 是通过 AssignedProject 连接模型提供的,这就是 project.user 将返回 nil 的原因。我正在努力解决的是分配项目创建者的最佳方式(顺便说一下,这不需要需要用户,它可能是< code>creator 或其他描述性内容,只要它是 User 类型)。

然后,我们的想法是创建一个从 User 返回 projects_created 的方法,该方法将仅选择该用户创建的项目。其中 user.projects 当然会返回与用户关联的所有项目。

假设这种关联相当常见,那么实现我想要的效果的最佳方法是什么?任何方向都将受到高度赞赏。

I have a User model which has many projects and a Project model which can have many users, but also belongs to a single user (ie the user who created this project). It must belong to a User. It also allows a list of users to be associated with it, think collaboration.

With this in mind, my models look like this:

class User < ActiveRecord::Base
  has_many :assigned_projects
  has_many :projects, :through => :assigned_projects
end

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :assigned_projects
  has_many :users, :through => :assigned_projects
end

class AssignedProject < ActiveRecord::Base
  belongs_to :user
  belongs_to :project
end

Now, when I want to create a new project through a User, this is how I would do it:

user = User.create(:name => 'injekt')
user.projects.create(:name => 'project one')

Now, I know that projects is provided through an AssignedProject join model, which is why project.user will return nil. What I'm struggling to get my head around is the best way to assign the project creator (which by the way doesn't need to be user, it could be creator or something else descriptive, as long as it is of type User).

The idea then is to create a method to return projects_created from a User which will select only projects created by this user. Where user.projects will of course return ALL projects a user is associated with.

Assuming this kind of association is fairly common, what's the best way to achieve what I want? Any direction is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴意少年 2024-11-24 16:27:58

将 Creator_id 列添加到项目表中以获取创建者关系,然后将关联添加到模型:

class User < ActiveRecord::Base
  has_many :assigned_projects
  has_many :projects, :through => :assigned_projects

  has_many :created_projects, :class_name => "Project", :foreign_key => :creator_id
end

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :assigned_projects
  has_many :users, :through => :assigned_projects

  belongs_to :creator, :class_name => "User", :foreign_key => :creator_id
end

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many

Add a creator_id column to your projects table for the creator relationship, and then add the associations to the models:

class User < ActiveRecord::Base
  has_many :assigned_projects
  has_many :projects, :through => :assigned_projects

  has_many :created_projects, :class_name => "Project", :foreign_key => :creator_id
end

class Project < ActiveRecord::Base
  belongs_to :user
  has_many :assigned_projects
  has_many :users, :through => :assigned_projects

  belongs_to :creator, :class_name => "User", :foreign_key => :creator_id
end

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many

鲸落 2024-11-24 16:27:58

我想对设计进行一些改进。我们实际上不需要中间模型,因为它不包含除 reference_ids 之外的任何额外列,因此 HABTM 关联最适合这里。

class User < ActiveRecord::Base
  has_and_belongs_to_many :projects, :join_table => :assigned_projects
  has_many :created_projects, :class_name => "Project", :foreign_key => :creator_id
end

class Project < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => :assigned_projects
  belongs_to :creator, :class_name => "User", :foreign_key => :creator_id
end

I wanted to add little improvement to design. We don't actually need intermediate model because it does not contain any extra column other than reference_ids hence HABTM association is best suited over here.

class User < ActiveRecord::Base
  has_and_belongs_to_many :projects, :join_table => :assigned_projects
  has_many :created_projects, :class_name => "Project", :foreign_key => :creator_id
end

class Project < ActiveRecord::Base
  has_and_belongs_to_many :users, :join_table => :assigned_projects
  belongs_to :creator, :class_name => "User", :foreign_key => :creator_id
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文