DEEEEPLY nested

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

:slight_smile:

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?

a == current_user.c.b.a

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?

thanks guys, all of u deserve 5 stars :slight_smile:

On Apr 16, 4:40 pm, Rob B. [email protected]

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
[email protected]