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
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.
In support of these model definitions would be the following tables/keys:
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!