Ruby on rails RoR定义两个模型之间有意义的关系
我想在rails项目中定义多对多关系。给个体关系赋予不同含义的最佳方式是什么Ruby on rails RoR定义两个模型之间有意义的关系,ruby-on-rails,many-to-many,has-many,semantics,Ruby On Rails,Many To Many,Has Many,Semantics,我想在rails项目中定义多对多关系。给个体关系赋予不同含义的最佳方式是什么 +------------+ has many +-------------+ | | ---------------------> | | | person | | project | | | <--------------------- |
+------------+ has many +-------------+
| | ---------------------> | |
| person | | project |
| | <--------------------- | |
+------------+ has many +-------------+
+-------------+有许多+-------------+
| | ---------------------> | |
|个人| |项目|
||人、项目和角色之间的关系应该是一张自己的表格。制作一个包含人员、项目和人员在该项目中的角色的作业类。然后Person有很多:项目,:到=>:assignments
人、项目和角色之间的关系应该是一个自己的表格。制作一个包含人员、项目和人员在该项目中的角色的作业类。然后Person有很多:项目,:到=>:assignments
最好的方法是创建一个富联接表
即:
处理此问题的最佳方法是创建一个富联接表
即:
谢谢你的回答!你有没有一些流行语给我,这样我可以读更多关于这个话题的内容?那太好了,我对这一切仍然很不放心。我建议你阅读Rails协会指南:谢谢你的回答!你有没有一些流行语给我,这样我可以读更多关于这个话题的内容?那太好了,我对这一切仍然很不放心。我建议你阅读Rails协会指南:哦,伙计,没关系!rails几乎每天都让我惊讶。。。(嗯,我是个新手)谢谢你的回答,这正是我想要的!在课堂电影中,你的意思是“有很多人,:通过=>:credits”,而不是“有很多人,:通过=>:jobs”,对吗?是的,我把它写成了jobs,然后意识到信用更有意义。所以我改了,一定是错过了。抱歉搞混了。哦,伙计,没关系!rails几乎每天都让我惊讶。。。(嗯,我是个新手)谢谢你的回答,这正是我想要的!在课堂电影中,你的意思是“有很多人,:通过=>:credits”,而不是“有很多人,:通过=>:jobs”,对吗?是的,我把它写成了jobs,然后意识到信用更有意义。所以我改了,一定是错过了。很抱歉给你带来了困惑。
| has many => | | <= has many |
Person | | Credit | | Movie
| <= belongs to | | belongs to => |
class Person < ActiveRecord::Base
has_many :credits
has_many :movies, :through => :credits, :unique => true
has_many :acting_credits, :class_name => "Credit",
:condition => "role = 'Actor'"
has_many :acting_projects, :class_name => "Movie",
:through => :acting_credits
has_many :writing_credits, :class_name => "Credit",
:condition => "role = 'Writer'"
has_many :writing_projects, :class_name => "Movie",
:through => :writing_credits
end
class Credit < ActiveRecord::Base
belongs_to :person
belongs_to :movie
end
class Movie < ActiveRecord::Base
has_many :credits
has_many :people, :through => :credits, :unique => true
has_many :acting_credits, :class_name => "Credit",
:condition => "role = 'Actor'"
has_many :actors, :class_name => "Person", :through => :acting_credits
has_many :writing_credits, :class_name => "Credit",
:condition => "role = 'Writer'"
has_many :writers, :class_name => "Person", :through => :writing_credits
end
@movie.actors # => People who acted in @movie
@movie.writers # => People who wrote the script for @movie
@person.movies # => All Movies @person was involved with
@person.acting_projects # => All Movies that @person acted in