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

  • SEARCH
  • Home
  • 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 3666504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:53:17+00:00 2026-05-19T01:53:17+00:00

I have a Rails 3 site I’m working on that has users who operate

  • 0

I have a Rails 3 site I’m working on that has users who operate on “documents.”

If each document had a user_id then for the edit method in my document controller I could restrict results to documents owned by the current user with something like:

@doc = current_user.documents.find(params[:id])
respond_with(@doc)

Unfortunately, it’s not that simple. Each user can have a relationship with a document by means of an intermediate “subscription” – they can be the owner, an editor, or a reader.

So now, in my edit method I want to be able to restrict the retrieved document to those who are an owner or an editor. How do I do this? Currently I have a scope in my Document model like:

scope :editable_by, lambda { |user| joins(:subscriptions).where("user_id = ? AND (subscriptions.role = ? OR subscriptions.role = ?)", user.id, Subscription::ROLES['owner'], Subscription::ROLES['editor']) }

so that I can then go in my edit method:

@doc = Document.editable_by(current_user).find(params[:id])

Is there a nicer way?

  • 1 1 Answer
  • 1 View
  • 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-19T01:53:18+00:00Added an answer on May 19, 2026 at 1:53 am

    I don’t think you’ll find a nicer way. For example, I tend to use CanCan for authorization, which only provides built-in helper methods if your model has an attribute indicating access rights (e.g. an owner_id).

    Otherwise, you still have to implement a scope or a method on your model. So unless I’m mistaken, you’ll be hard pressed to find a better way than what you currently have.

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

Sidebar

Related Questions

I have a Rails 2.1.2 site that only has html templates e.g. jobs.html.erb, so
I am working on a Ruby on Rails site that is going to have
I have a rails app that gives users a subdomain for their site. Users
I have a site built in Ruby On Rails which has many ads in
I have a rails site with two models: events and dates events has many
I'm working on a Rails site that connects to an Oracle database, and though
I have a simple animation on my rails site that pop's a div (with
We have a rails site that is expanding quite a bit. it is using
I have a rails app that is currently an affiliate site with my customer's
For the site I am currently working on we have 2 Rails 3.2 projects.

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.