Really stuck on nested resources

I have nested resources 2 levels deep and am having problems with the
controller of the deepest resource;

Parent > Child > Dog

The rails guide says this for the forms, which seems to work fine:

form_for [:parent, :child, @dog]

I have all the appropriate belong_to and has_many but I cannot figure
out how to do this in the dog controller:

def create
parent = Parent.find(params[:parent_id])
child = Child.find(params[:child_id])
@record= parent.child.dog.build(params[:dog])

end

I am getting this error: undefined method `child’

Could this be a pluralization issue?
Did you set up your models as:

class Parent < ActiveRecord::Base
has_many :children
end

class Parent < ActiveRecord::Base
has_many :children
belongs_to :parent
end

class Dog < ActiveRecord::Base
has_many :dogs
belongs_to :child
end

If so, then I would think that something like this might work:

parent = Parent.find(params[:parent_id])
child = parent.children.find(params[:child_id])
@record= child.dogs.build(params[:dog])

…but I am still very much a RoR neophyte. Everything I know about
nested routes I learned from
Rolling with Rails 2.0 - The First Full Tutorial - Part 1 | AkitaOnRails.com,
and I refer back to there each time I try to do it again.

–wpd

Thanks for the reply. My models are correct, they work find at the
higher levels of the hierarchy.

On Sep 18, 10:49 am, mlittle [email protected] wrote:

out how to do this in the dog controller:

def create
parent = Parent.find(params[:parent_id])
child = Child.find(params[:child_id])
@record= parent.child.dog.build(params[:dog])

end

I am getting this error: undefined method `child’

Not sure what the local variable “child” is doing? It’s not being
used in the 3rd line.

I would have expected the code to look like this:

parent = Parent.find(params[:parent_id])
child = parent.children.find(params[:child_id])
@dog = child.dogs.build(params[:dog])

This way, you’re making sure that the child you find really belongs to
the parent. Then, you build a new dog for that child’s dog
collection.

Does this help?

Jeff
purpleworkshops.com

2009/9/20 mlittle [email protected]:

really don’t know how to do this. I’ve read some articles that say
more then 1 level deep is not a good idea but I don’t how to make sure
records aren’t viewed by other users that they didn’t create. I’ve
bought more book then I probably should and am getting frustrated.
Maybe my method of making sure records are not viewed by other users
is the wrong approach.

I am still confused about your model relationships. Could you post
the class definitions please showing the has and belongs to
relationships?

Colin

Hey mlttle-

You can create a new dog with:

def create
@dog = Parent.find(params[:parent_id]).children.find(params
[:child_id]).dogs.create
end

The console is your best friend to quickly diagnose errors:

@record= parent.child.dog.build(params[:dog])
NoMethodError: undefined method `child’ for #Parent:0x22b6748

… becuase your parent model has many children

@record= parent.children.find(child).dogs.build(params[:dog])

Michael

Maybe I’m just going about my application in the wrong way. I’m new
with rails and I’m trying to make sure that Child and Dog can only be
viewed my the person who creates them. That’s how I ended up with
resources 3 deep. My resources are actually called User, Child and
Parent. User is also current_user (I use restful_authentication) and
all the resources are created by the current_user. My routes are basic
but are 3 resources deep: User > Child > Parent. So, I reference each
in my controllers by current_user.child and when I started trying to
access current_user.child.parent, things started to break because I
really don’t know how to do this. I’ve read some articles that say
more then 1 level deep is not a good idea but I don’t how to make sure
records aren’t viewed by other users that they didn’t create. I’ve
bought more book then I probably should and am getting frustrated.
Maybe my method of making sure records are not viewed by other users
is the wrong approach.

MichaelB:

THANK YOU!!! So much. Your solution fixed my problem and taught me a
bunch about rails.

Thanks again for your help.

One other way to think about this - you shouldn’t need to nest
resources more then one level deep.

You only deal with children in relation to parents and dogs in
relation to children. Separate relationships which can be handled
transparently. Plus the links you have to generate get a lot more
confusing - parents_childrens_dog_path( parent_id, child_id, dog_id)
or some such…

Agoofin:

The only reason I am doing it this way is for the current_user. This
is the only way I know how to make sure another user does not see a
record created by someone else. If I knew another way to do this then
I would avoid the nested resources. I can see where my current model
is going to become a pain to manage. If I knew how to do that I would
change my ways. Currently I do:

current_user.children.find(child).dogs.build(params[:dog])

I know there has to be a better way but I am glad it’s working now. I
am new and am sure as I improve I will discover the appropriate way to
handle this.

Thanks for the feedback.

MLittle,
I just picked up this thread towards the bottom and thought maybe I
could say one thing.
Don’t ever nest more than two levels deep. I have some amazingly
‘deep’ applications and suffered the pains of hell until I picked up
this important point.
If you are still treading water, send me an email and I’ll send a
routes file that will make this point more clear.
Good luck,
David