On Apr 16, 2010, at 4:17 PM, David Zhu 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 Development with Rails" from the Pragmatic Bookshelf.


On Apr 16, 4:13 pm, Sharagoz <[email protected]> wrote:
a == current_user.c.b.a

On Apr 16, 9:06 pm, David Zhu <[email protected]> wrote:



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

   <p>This post belongs to another user. Please navigate away
immediately.</p>

<% 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?

--

Rob Biedenharn          http://agileconsultingllc.com
[email protected]



--
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to