Rails 有很多并且属于一个
我有一个 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>creatorUser
类型)。
然后,我们的想法是创建一个从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 Creator_id 列添加到项目表中以获取创建者关系,然后将关联添加到模型:
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:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
我想对设计进行一些改进。我们实际上不需要中间模型,因为它不包含除 reference_ids 之外的任何额外列,因此 HABTM 关联最适合这里。
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.