# Relationship in Models

**URL:** <https://www.ruby-forum.com/t/relationship-in-models/181196>\
**Category:** Rails\
**Created:** [January 7, 2010, 6:54am UTC](https://www.ruby-forum.com/t/relationship-in-models/181196 "2010-01-07T06:54:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sushi2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@sushi2k](https://www.ruby-forum.com/u/sushi2k)\
**Post date:** [January 7, 2010, 6:54am UTC](https://www.ruby-forum.com/t/relationship-in-models/181196/1 "2010-01-07T06:54:03Z")

</div>

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:  
`
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
`

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

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

projects\_ressources.rb:  
`
class ProjectsRessource < ActiveRecord::Base
belongs_to :ressource
belongs_to :project
belongs_to :role
end
`

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:

`
NoMethodError: undefined method `roles' for #
        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?

---

<div class="post-metadata">

**Author:** ![sushi2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@sushi2k](https://www.ruby-forum.com/u/sushi2k)\
**Post date:** [January 7, 2010, 8:53am UTC](https://www.ruby-forum.com/t/relationship-in-models/181196/2 "2010-01-07T08:53:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sushi2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@sushi2k](https://www.ruby-forum.com/u/sushi2k)\
**Post date:** [January 7, 2010, 12:39pm UTC](https://www.ruby-forum.com/t/relationship-in-models/181196/3 "2010-01-07T12:39:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sushi2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@sushi2k](https://www.ruby-forum.com/u/sushi2k)\
**Post date:** [January 7, 2010, 12:39pm UTC](https://www.ruby-forum.com/t/relationship-in-models/181196/4 "2010-01-07T12:39:13Z")

</div>

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 G.  
Company: [www.innozon.com](http://www.innozon.com), Hyderabad

---

<div class="post-metadata">

**Author:** ![sushi2k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@sushi2k](https://www.ruby-forum.com/u/sushi2k)\
**Post date:** [January 8, 2010, 2:15am UTC](https://www.ruby-forum.com/t/relationship-in-models/181196/5 "2010-01-08T02:15:07Z")

</div>

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 G.  
> Company: [www.innozon.com](http://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  
🙂 This is now all clear to me.
