Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6318095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:36:06+00:00 2026-05-24T15:36:06+00:00

I have run into an issue regarding how to identify which user owns particular

  • 0

I have run into an issue regarding how to identify which user owns particular resources so that I can prevent inappropriate access to them.

I have the following nested associations:

User has many
Profiles has one
SamplePage has many
Subjects

Once they become nested this deep it’s become very unwieldy to access the user object via the associations and then compare that to current user e.g.:

@subject.sample_page.profile.user == current_user

I’ve read that a better way of restricting access is to scope the retrieval of a model to the current user. e.g:

@profile = current_user.profiles.find(params[:id])

That makes a lot of sense to me but how would I do a similar thing to get a Subject back? I’ve not found any examples that used nested associations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:36:07+00:00Added an answer on May 24, 2026 at 3:36 pm

    not sure to understand what you want to do, and not sure i can help you since i’m a huge noob, but i would try something like this (assumed that current_user returns a User):

    class Profile < ActiveRecord::Base
      has_many :subjects, :through => :sample_pages
    end
    

    and in your controller:

    @subject = current_user.profiles.subjects.find(params[:id])
    

    more handy this way:

    class User < ActiveRecord::Base
      def subjects
        profiles.subjects
      end
    end
    
    @subject = current_user.subjects.find(params[:id])
    

    all of this should be lazy loaded, as explained here : http://asciicasts.com/episodes/202-active-record-queries-in-rails-3

    however, if it is a frequent operation, you may want to redesign things a bit, as long chains of associations mean heavy queries (lots of joins).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently run into a particularly sticky issue regarding committing the result of
I have run into an issue with WPF and Commands that are bound to
I've run into an issue that I can't seem to figure out the solution
I have run into an issue that is probably due to my mis-understanding of
I have run into an issue in which IE does not open up the
I'm working on some screen scraping software and have run into an issue with
a day before end of project i run into a complicated issue: i have
I have run into a couple of similar quirks regarding uint usage in both
I have a general question - regarding testing of values, I have run into
I have run into an issue when creating a object type in Oracle 10g

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.