# Map.resource :has\_many :through :has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/map-resource-has-many-through-has-and-belongs-to-many/123645>\
**Category:** Rails\
**Created:** [December 19, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/map-resource-has-many-through-has-and-belongs-to-many/123645 "2007-12-19T13:38:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dfb087/32.png) [@ewan](https://www.ruby-forum.com/u/ewan)\
**Post date:** [December 19, 2007, 1:38pm UTC](https://www.ruby-forum.com/t/map-resource-has-many-through-has-and-belongs-to-many/123645/1 "2007-12-19T13:38:20Z")

</div>

Hello,  
I’ve got the following model

objectA 1 \<------\> 0…\* objectB 0…\* \<-------\> 1 objectC

I’d like the url  
GET /objectA/1/objectC  
to show all objectC associated with objectA through objectB

In routes.rb  
map.resources :objectAs, :has\_many =\> [:objectCs]  
or  
map.resources :objectAs do |:objectA|  
objectA.resources :objectCs  
end

shows me all objectCs not just those associated with objectA through  
objectB

How can I get all objectCs associated with objectA using the url GET /  
objectA/1/objectC?  
Also if I had the following model  
objectA 0…\* \<-------\> 0…\* objectC  
How can I get all objectCs associated with objectA using the url GET /  
objectA/1/objectC?

Thanks,  
ewan

---

<div class="post-metadata">

**Author:** ![ewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dfb087/32.png) [@ewan](https://www.ruby-forum.com/u/ewan)\
**Post date:** [December 19, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/map-resource-has-many-through-has-and-belongs-to-many/123645/2 "2007-12-19T19:04:00Z")

</div>

map.resources :objectAs do |:objectA|  
objectA.resources :objectCs  
end

this should work, after correcting this typo: |:objectA| should  
|objectA|

then in your action:  
oa = ObjectA.find(params[:objectA\_id])

the objectC should simply be:  
oa.objectCs

if you have setup the has\_many and belongs\_to stuff in the models

but:  
your naming conventions are very bad for RoR (if this is not for fast  
typing this example). you will run in lots of trouble that way and i’m  
not sure, if the stuff would work the way i typed it.

variable names are always lowercase eg: object\_a  
only classnames are camelcase eg: ObjectA

and it’s important to keep the rules for singular/plural namings

---

<div class="post-metadata">

**Author:** ![ewan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/dfb087/32.png) [@ewan](https://www.ruby-forum.com/u/ewan)\
**Post date:** [December 19, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/map-resource-has-many-through-has-and-belongs-to-many/123645/3 "2007-12-19T19:10:56Z")

</div>

Thorsten M. wrote:

> map.resources :objectAs do |:objectA|

> but:  
> your naming conventions are very bad for RoR (if this is not for fast  
> typing this example). you will run in lots of trouble that way and i’m  
> not sure, if the stuff would work the way i typed it.
> 
> variable names are always lowercase eg: object\_a  
> only classnames are camelcase eg: ObjectA
> 
> and it’s important to keep the rules for singular/plural namings

i did not know you could do that with routes.rb  
i always had it in the model
