帮助在Rails中构建模型

时间:2011-05-23 23:28:17

标签: ruby-on-rails ruby-on-rails-3

class Profile
  has_many :projects, :through => "teamss"
  has_many :teams, :foreign_key => "member_id"
  has_many :own_projects, :class_name => "Project", :foreign_key => :profile_id
  has_many :own_teams, :through => :own_projects, :source => :teams
end

class Project
  belongs_to :profile, :class_name => "Profile"
  has_many :teams
  has_many :members, :class_name => "Profile", :through => "teams", :foreign_key => "member_id"
end

class Team
  belongs_to :member, :class_name => 'Profile'
  belongs_to :project
end

我需要创建模型Evaluation。我想要做的是在项目的每个成员(包括所有者)的project#view页面中生成一个链接,以便创建一个Evaluation该人将点击该链接并评估与该相关的人员。这个链接。 Project的所有者将评估所有成员,并且所有成员将评估所有者。

我已将模型Evaluation定义如下,但我想我错过了一些内容:

class Evaluations < ActiveRecord::Base
  belongs_to :evaluated, :class_name => 'Profile', :foreign_key => "evaluated_id"
  belongs_to: :profile, :class_name => 'Profile', :foreign_key => "profile_id"
end

记住Evaluation表将拥有大量属性,这就是为什么我不会使用has_many_and_belongs_to_many

如何创建此模型以实现我想要的功能并能够通过project#show页面访问我需要的所有内容?

谢谢!

被修改

完成的更改:

class Profile
  has_many :evaluations, :dependent => :destroy, :foreign_key => :evaluation_id
  has_many :evaluators, :through => :evaluations, :foreign_key => :profile_id
end

class Project
  has_many :evaluations,:foreign_key => "project_id"
end

class Evaluations < ActiveRecord::Base
  belongs_to :evaluated, :class_name => 'Profile', :foreign_key => "evaluated_id"
  belongs_to: :profile, :class_name => 'Profile', :foreign_key => "profile_id"
  belongs_to: :project, :class_name => 'Project', :foreign_key => "project_id"
end

1 个答案:

答案 0 :(得分:0)

您的团队和个人资料(成员)协会有点混乱。团队有许多个人资料(成员),个人资料(成员)属于团队。除此之外,你可能只是过于复杂化了。我很好奇为什么你不会只调用Profile Model会员并让事情变得更简单?

话虽如此,你正在做一组非常复杂的联想。我强烈建议您仔细阅读ActiveRecord Associations doc以深入了解这些内容是如何工作的,特别是主键和外键如何自动确定。

我认为下面会得到你想要的东西。

class Team
  has_and_belongs_to_many :members,
    :class_name => "Profile"
  belongs_to :project
  belongs_to :team_leader,
    :class_name => "Profile"
end

class Profile
  has_and_belongs_to_many :teams
  has_many :projects,
    :through => :teams
  has_many :owned_projects,
    :class_name => "Project",
    :foreign_key => "owner_id"
  has_many :owned_teams,
    :class_name => "Team",
    :foreign_key => "team_leader_id"
  has_many :evaluations
  has_many :evaluations_given,
    :class_name => "Evaluation",
    :foreign_key => "evaluator_id"
end

class Project
  has_many :teams
  belongs_to :owner,
    :class_name => "Profile"
  has_many :members,
    :through => :teams
  has_many :evaluations
end

class Evaluation
  belongs_to :profile
  belongs_to :evaluator,
    :class_name => "Profile",
    :foreign_key => "evaluator_id"
  belongs_to :project
end

支持这些模型定义的是以下表/键:

evaluation
  id
  profile_id
  evaluator_id
  project_id

teams
  id
  project_id
  team_leader_id

profiles
  id

project
  id
  owner_id

profiles_teams
  profile_id
  team_id

通过这种方式,您应该能够访问正确链接所需的集合/对象,例如:

  • project.members

    project.owner

    project.teams

    team.project

    profile.projects

    profile.owned_projects

    profile.teams

    team.members

    team.leader

    project.evaluations

    profile.evaluations

    evaluation.evaluator

    profile.evaluations_given

另请注意,如果您尝试使用 RESTFUL 框架,那么嵌套资源将会同样复杂。阅读!