[Rails] Re: Really stuck on nested resources

2009-09-21 Thread InventoryTrackers

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

On Sep 20, 9:35 pm, mlittle mdlit...@gmail.com wrote:
 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.

 On Sep 20, 5:32 pm, AGoofin amor...@gmail.com wrote:



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

  On Sep 20, 1:41 pm, mlittle mdlit...@gmail.com wrote:

   MichaelB:

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

   Thanks again for your help.

   On Sep 20, 8:32 am, MichaelB michael.james.bamf...@gmail.com wrote:

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- Hide quoted text -

 - Show quoted text -
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Really stuck on nested resources

2009-09-20 Thread Colin Law

2009/9/20 mlittle mdlit...@gmail.com:

 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.


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

Colin

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Really stuck on nested resources

2009-09-20 Thread MichaelB

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



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Really stuck on nested resources

2009-09-20 Thread AGoofin

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

On Sep 20, 1:41 pm, mlittle mdlit...@gmail.com wrote:
 MichaelB:

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

 Thanks again for your help.

 On Sep 20, 8:32 am, MichaelB michael.james.bamf...@gmail.com wrote:

  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
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Really stuck on nested resources

2009-09-20 Thread mlittle

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.

On Sep 20, 5:32 pm, AGoofin amor...@gmail.com wrote:
 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...

 On Sep 20, 1:41 pm, mlittle mdlit...@gmail.com wrote:

  MichaelB:

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

  Thanks again for your help.

  On Sep 20, 8:32 am, MichaelB michael.james.bamf...@gmail.com wrote:

   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
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Really stuck on nested resources

2009-09-19 Thread mlittle

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.

On Sep 18, 2:01 pm, Jeff cohen.j...@gmail.com wrote:
 On Sep 18, 10:49 am, mlittle mdlit...@gmail.com wrote:



  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'

 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
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Really stuck on nested resources

2009-09-18 Thread Patrick Doyle

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
http://akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-full-tutorial,
and I refer back to there each time I try to do it again.

--wpd

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Really stuck on nested resources

2009-09-18 Thread mlittle

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

On Sep 18, 9:01 am, Patrick Doyle wpds...@gmail.com wrote:
 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 
 fromhttp://akitaonrails.com/2007/12/12/rolling-with-rails-2-0-the-first-f...,
 and I refer back to there each time I try to do it again.

 --wpd
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] Re: Really stuck on nested resources

2009-09-18 Thread Jeff

On Sep 18, 10:49 am, mlittle mdlit...@gmail.com wrote:
 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'

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

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Ruby 
on Rails: Talk group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---