Help to build a Model in Rails

2019-07-07 03:10发布

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

I need to create model Evaluation. What I want to do is generate a link in project#view page for each member of the project, including the owner, in order to make an Evaluation The person will click on the link and evaluate the person related to this link. The owner of the Project will evaluate all the members, and all the members will evaluate the owner.

I have defined model Evaluation as following, but I think I miss something:

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

Remembering that Evaluation table will have tons of attributes, that's why I'm going not going with has_many_and_belongs_to_many.

How can I create this model in order to do what I want and be able to acess all I need via project#show page?

Thanks!

Edited

Changes done:

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条回答
成全新的幸福
2楼-- · 2019-07-07 03:41

You have your team and profile (member) associations a little messed up. A team has many profiles (members), and a profile (member) belongs to a team. Beyond that, you may just be over-complicating things. I'm curious as to why you wouldn't just call the Profile Model Member instead and keep things that much simpler?

That being said, you are doing a pretty complex set of associations. I would highly recommend you read the ActiveRecord Associations doc closely to get a firm understanding of how these things work, especially how primary and foreign keys are automagically determined.

I think below will get you what you want.

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

In support of these model definitions would be the following tables/keys:

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

With this, you should be able to access collections/objects that you need to get your links right such as:

  • 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

Note, also, that your nested resources are going to be just as complex, if you try to stay with a RESTFUL framework. Read up!

查看更多
登录 后发表回答