Map.resource :has_many :through :has_and_belongs_to_many

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs