# Relationship in a Volunteers Project

**URL:** <https://www.ruby-forum.com/t/relationship-in-a-volunteers-project/147953>\
**Category:** Rails\
**Created:** [September 7, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/relationship-in-a-volunteers-project/147953 "2008-09-07T15:46:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dazuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dazuma](https://www.ruby-forum.com/u/dazuma)\
**Post date:** [September 7, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/relationship-in-a-volunteers-project/147953/1 "2008-09-07T15:46:11Z")

</div>

Hi! I’m new in Rails and I’ve started my own project this week. I’m  
working with Rails 2.0 and REST. I want to define some relationships  
but I’m stuck with REST routes and some other things.

My project aims to manage a team of volunteers. I designed three  
models, Month, User and Shifts. A Month have many users and many  
shifts. A User have many months and many shifts, while a Shift have  
one user and one month. I want to define this relationship in some way  
to work with this different routes.

/month/1/user/5/shifts/ Show shifts that belongs to month with  
id=1 and where user=5  
/month/1/user/5/shift/3 Show shift with id=3 that belongs to  
month with id=1 and where user=5

/user/2/months Show months that belongs to user  
with id=2  
/user/3/month/2 Show month with id=2 that belongs to  
user with id=3  
/user/4/month/2/shifts Show shifts that belongs to user with  
id=4 and where month=2  
/user/4/month/2/shift/3

I thought in this relationship but I can’t understand completely how  
this will work.

#Model user.rb

has\_many :shifts  
has\_many :months, :through =\> :shifts

#Model month.rb

has\_many :shifts  
has\_many :users, :through =\> :shifts

#Model shifts.rb

belongs\_to :month  
belongs\_to :user

I know I’ve to define a middle table to manage this kind of  
relationship but I don’t know how. Also I’ve to define nested routes,  
but the problem is how, because I want it can work with all this  
different routes. Moreover, I want to know how I should modify my  
Shifts controller, so when I’ll create a new shift it will be related  
to a month an a user. Thanks in advance. I know this is such a general  
question, but I would appreciate any kind of help.

---

<div class="post-metadata">

**Author:** ![dazuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dazuma](https://www.ruby-forum.com/u/dazuma)\
**Post date:** [September 9, 2008, 2:45am UTC](https://www.ruby-forum.com/t/relationship-in-a-volunteers-project/147953/2 "2008-09-09T02:45:40Z")

</div>

maybe try something like this for your routing

map.connect ‘month/:month\_id/user/:user\_id/shifts’, :controller =\>  
‘shifts’, :action =\> ‘index’, :conditions =\> { :method =\> :get }  
map.connect ‘month/:month\_id/user/:user\_id/shifts/:id’, :controller =\>  
‘shifts’, :action =\> ‘show’, :conditions =\> { :method =\> :get }

map.connect ‘user/:user\_id/months’, :controller =\> ‘months’, :action  
=\> ‘index’, :conditions =\> { :method =\> :get }  
map.connect ‘user/:user\_id/months/:id’, :controller =\>  
‘months’, :action =\> ‘show’, :conditions =\> { :method =\> :get }

Then in your controller you will need to do something like:  
def index  
@shifts = Shift.find(:all, :conditions =\> [‘user\_id = ? AND month\_id  
= ?’, [params[:user\_id], params[:month\_id]])  
end

I strongly recommend that you take a look at rspec’s default routing  
specs. This is easiest to do if you install RSpec into a new rails  
app, and then run ‘ruby script/generate rspec\_scaffold’. You  
definitely don’t want to skip your routing tests when you are doing  
complex routing like this.

I could be wrong, but nested routes _might_ be more hassle than it is  
worth since this is a many to many relationship.

HTH

---

<div class="post-metadata">

**Author:** ![dazuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dazuma](https://www.ruby-forum.com/u/dazuma)\
**Post date:** [September 11, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/relationship-in-a-volunteers-project/147953/3 "2008-09-11T19:16:25Z")

</div>

Thanks for your help. I’ll try it.
