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 3272158
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:47:52+00:00 2026-05-17T18:47:52+00:00

I have a model that looks something like this: class Comment < ActiveRecord::Base …

  • 0

I have a model that looks something like this:

class Comment < ActiveRecord::Base
  ...
  #allow editing comment if it is moderated and the user passed-in
  #is the one that owns the comment
  def can_edit?(user)
    moderated? and user.Type == User and user.id == self.user_id
  end
  ...
end

And a call in a view:

<%= link_to 'Show Comment', @comment if @comment.can_show?(current_user) %>

I need to write many such methods in many different models – sort of validation checks to see if current_user is allowed to
do something on a model.

But it feels cumbersome – especially the need to check that the passed-in user is indeed a object of type User.

What’s a clean, best-practice way to do this sort of thing? Am I on the right track? (i.e. should I be adding such methods to a model or somewhere else)

Note

I am using scoped queries to get the comments and other models, but in some cases I cannot scope the query so I have to use the can_xxxx? methods

Ps. Is what I’m doing considered a "fat model"?

  • 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-17T18:47:53+00:00Added an answer on May 17, 2026 at 6:47 pm

    Create a module containing all the authorization methods and include the module to all the classes requiring authorization.

    Add a file called authorization.rb to app/models directory.

    module Authorization
    
      def can_edit?(user)
        moderated? and user.is_a?(User) and user.id == self.user_id
      end
    
      def self.included(base)
        base.send(:extend, ClassMethods)
      end
    
      module ClassMethods
        # add your class methods here.
      end
    end
    

    Add a file called authorization.rb to config/initializers directory.

    %w(
     Comment
     Post
    ).each do |klass|
      klass.constantize.include(Authorization)
    end
    

    Now Comment and Post models will have all the authorization methods.

    Other approach is to use your current named_scope.

    class Post
      named_scope :accessible, lambda { |user|
        {
          :conditions => { :user_id => user.id, :moderated => true}
        }
      }
    end
    

    Post controller actions

    class PostsController
    
      def index
        @posts = Post.acessible(current_user)
        # process data
      end
    
      def show
        # throws record not found when the record is not accessible.
        @post = Post.acessible(current_user).find(params[:id])
        # process data
      end
    
    end
    

    I like this approach as it uses the same logic for accessing an array of objects or a single object.

    You can add the named_scope to the module to avoid repeated definitions:

    module Authorization
      def self.included(base)
        base.named_scope :accessible, lambda { |user|
          {
            :conditions => { :user_id => user.id, :moderated => true}
          }
        }
      end
    
      module ClassMethods
        # add your class methods here.
      end
    end
    

    Make sure to include the module in required classes as suggested earlier.

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

Sidebar

Related Questions

I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a Model that looks something like this: class Business(models.Model): name = models.CharField('business
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
Specifically, I have a model that has a field like this pub_date = models.DateField(date
I have an ActiveRecord model that I would like to convert to xml, but
I am trying to use Windows Workflow and have a model that looks similar
I have a data model that includes common columns like addedBy, editedby (user), addedDate,
So I have a User model that :has_many other models like Documents, Videos, Posts
I have a relationship in a Core Data model that feels like it wants
I have a Person model that has a foreign key relationship to Book ,

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.