- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsI'm new to rails and I'm having problems figuring out how to limit the ability to for 1 user to see the records that another user creates. I have Users and Children and I want to make it so a User with user_id of 1 who creates children_id of 8,9 can only see children 8,9. I also want to make it so User_id 2 cannot see user_id 1 or children 8 and 9. I am using restful_authentication.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: raasdnilPosted on 2009-08-31 at 17:48:19ID: 25227721
My guess from your question is that you already have an has_many association from the User => Child model. ie, something like:
(params[:i d])
class User < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :user
end
Given this, you probably are trying to work out how to make sure a user only can ask for the URLs to their own children.
If this is the case, you are trying to find the code for the following snippet:
ChildrenController < ActionController::Base
def show
@child = Child.find(params[:id]
end
end
If this is your current code, then the user can ask for any child, theirs or not.
To fix this, use the User => Child has_many association:
ChildrenController < ActionController::Base
def show
@child = current_user.children.find
end
end
This way the User can only look at children that can be found from within the user's children collection.
I don't use Restful Athentication at the moment, but I believe "current_user" returns the current user object from the database.
Hope that helps
Mikel