Relationship in Models

Hello, I'm doing right now my first steps in Ruby on Rails and I've got a problem. Just some explanation: A project can have ressources (like an employee) and this ressource can have certain roles (e.g. a team member, or a team leader). As you can see in the attachment, I have a Table called Projects_Ressources. This Table has three relationships (belongs_to). One to Projects, one to Ressources and one to Roles.

This is my model description for,

role.rb: <code> class Role < ActiveRecord::Base   has_many :projects_ressources   has_many :ressources, :through => :projects_ressources   has_many :permissions_roles   has_many :permissions, :through => :permissions_roles end </code>

project.rb: <code> class Project < ActiveRecord::Base   has_many :projects_ressources   has_many :ressources, :through => :projects_ressources end </>

ressource.rb: <code> class Ressource < ActiveRecord::Base   has_many :projects_ressources   has_many :projects, :through => :projects_ressources   has_many :roles, :through => :projects_ressources end </code>

projects_ressources.rb: <code> class ProjectsRessource < ActiveRecord::Base   belongs_to :ressource   belongs_to :project   belongs_to :role end </code>

When I'm now in the console, I can get for example the project with id 1

pro = Project.find(1)

And can get all the ressources to this certain project:

employee = pro.ressources

But I want also to know the roles of this ressources. How can I get now the roles of the ressources to this project with id 1? This doesn't work:

role = pro.roles

produces this error in the console:

<code> NoMethodError: undefined method `roles' for #<Project:0x4dffdc0>         from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record /attribute_methods.rb:260:in `method_missing'         from (irb):47 </code>

Is the design of the relationship wrong or what am I missing?

Attachments: http://www.ruby-forum.com/attachment/4360/erm.PNG

As seen from above in Project class there is no relation ship defined for role so how can you expect the result from it.

Add has_many :roles, :through => :projects_ressources and the required output will be produced.

Hi !

According to your relationships: resource :has_many => roles but you are using pro.roles. It means project should has many roles.Its totally wrong accordingly.

First fetch all project_resources for a particular project. Eg.

pr_resources = Project.find(1).project_resources

pr_resources will be a array of pr_resources

Now you can fetch any employee(resource) and its role associated with project id 1.

for eg. for any project_resource:

   resource = pr_resource.resource    role = pr_resource.role

Thanks,

But I want also to know the roles of this ressources. How can I get now the roles of the ressources to this project with id 1? This doesn't work:

role = pro.roles

produces this error in the console:

NoMethodError: undefined method `roles' for #<Project:0x4dffdc0>         from c:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record /attribute_methods.rb:260:in `method_missing'         from (irb):47         from :0

Is the design of the relationship wrong or what am I missing?

I just started the ruby server again and now it's working. Maybe a .rb file wasn't saved or something like that. I get now every role of every ressource that is stored in project_ressources. Thx anyway.

Ankur wrote:

Hi !

According to your relationships: resource :has_many => roles but you are using pro.roles. It means project should has many roles.Its totally wrong accordingly.

First fetch all project_resources for a particular project. Eg.

pr_resources = Project.find(1).project_resources

pr_resources will be a array of pr_resources

Now you can fetch any employee(resource) and its role associated with project id 1.

for eg. for any project_resource:

   resource = pr_resource.resource    role = pr_resource.role

Thanks,

-- ~ Ankur Gupta   Company: www.innozon.com, Hyderabad

I made already an update of the project.rb, because I found out that the rule was missing. That's why it worked for ressources but not for projects. Thx a lot for your explanation, I'm still in the learning process of RoR :slight_smile: This is now all clear to me.