"member_id" has_many :ow...">

Help creating a model in Rails

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 a model Evaluation. I want to create a link on the page project#viewfor each project participant, including the owner, to do Evaluation. The person clicks on the link and appreciates the person associated with this link. The owner Projectwill evaluate all participants, and all participants will evaluate the owner.

I defined the model Evaluationas follows, but I think something was missing:

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 there Evaluationwill be a lot of attributes in the table , so I'm not going to go with has_many_and_belongs_to_many.

How can I create this model to do what I want and be able to do everything I need on the page project#show?

Thank!

Edited

Changes made:

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
+3
source share
1

(), . (), () . , . , ?

, . 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. !

0

All Articles