# DEEEEPLY nested

**URL:** <https://www.ruby-forum.com/t/deeeeply-nested/187096>\
**Category:** Rails\
**Created:** [April 16, 2010, 9:07pm UTC](https://www.ruby-forum.com/t/deeeeply-nested/187096 "2010-04-16T21:07:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 9:07pm UTC](https://www.ruby-forum.com/t/deeeeply-nested/187096/1 "2010-04-16T21:07:39Z")

</div>

Hello,

i have “a” has many “b”, “b” has many “c”, and “c” has many “d”

Now… “d” is a user model, and I have a current\_user helper method  
defined.

If i navigate to an “a” show page, how can I make sure that my  
current\_user (“d”) is part of that “a”? meaning how can i make sure  
that current\_user belongs to “a”? Because there is the b, and c in  
between the “d” and “a”, how can I check to see if my current\_user  
(“d”) belongs to “a”?

And I being clear? thanks

🙂

P.S.

heres an example of what I want if there is only one level of  
associations–

* * *

\<% if !current\_user == @post.user %\>

This post belongs to another user. Please navigate away immediately.

\<% else %\>

# The content of edit.html.erb

\<% end %\>

* * *

but that only compares the user, with the model that is up one level.  
(In this case post)

How can i check to see if current\_user is part of a higher level?

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 10:14pm UTC](https://www.ruby-forum.com/t/deeeeply-nested/187096/2 "2010-04-16T22:14:24Z")

</div>

a == current\_user.c.b.a

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/deeeeply-nested/187096/3 "2010-04-16T22:18:30Z")

</div>

thank you sharagoz! But i have a question- is a, b, c supposed to  
instance variables? (should i have the @ in front of them, and then  
define them in the contrller?) or does rails know what tables I’m  
reffering to already?

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 17, 2010, 1:31am UTC](https://www.ruby-forum.com/t/deeeeply-nested/187096/4 "2010-04-17T01:31:06Z")

</div>

thanks guys, all of u deserve 5 stars 🙂

On Apr 16, 4:40 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 16, 2010, 10:42pm UTC](https://www.ruby-forum.com/t/deeeeply-nested/187096/5 "2010-04-16T22:42:30Z")

</div>

On Apr 16, 2010, at 4:17 PM, David Z. wrote:

> thank you sharagoz! But i have a question- is a, b, c supposed to  
> instance variables? (should i have the @ in front of them, and then  
> define them in the contrller?) or does rails know what tables I’m  
> reffering to already?

So flipping the associations around,

d belongs\_to c  
c belongs\_to b  
b belongs\_to a

Implying that your user model (d) contains a c\_id (and only associates  
to a single c)

If that’s really true, then you don’t even let the /show/a request  
complete (assuming that you have a login required filter otherwise you  
have to check logged\_in? or whatever makes current\_user valid).

def show  
a = A.find\_by\_id(params[:id])  
if current\_user.c.b.a != a  
redirect\_to :action =\> ‘show’, :id =\> current\_user.c.b.a  
end  
end

Of course, you can choose to redirect somewhere else, put an error  
page up, or anything else that suits you.

Your question about whether a, b, and c are instance variables reveals  
that you need to do some very basic learning about Rails and  
ActiveRecord associates in particular.

-Rob

P.S. In fact, there’s an alternate way to structure the find against A  
that you should discover while you learn Rails. There’s a beta  
version of the next edition of “Agile Web D. with Rails” from  
the Pragmatic Bookshelf.

> > > ## i have “a” has many “b”, “b” has many “c”, and “c” has many “d” And I being clear? thanks \<% if !current\_user == @post.user %\>
> > 
> > > but that only compares the user, with the model that is up one  
> > > level.  
> > > (In this case post)
> > 
> > > How can i check to see if current\_user is part of a higher level?
> > 
> > > –

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
