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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:22:10+00:00 2026-06-04T21:22:10+00:00

In my application using CanCan I have permissions where users can view and create

  • 0

In my application using CanCan I have permissions where users can view and create stores but I also want them to only be able to edit the ones they’ve created. Users can create as many stores as they like, which all should be editable by them. A store doesn’t have users so how could I do this when theirs no user_id apart of the Store table?

CanCan:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new 
    if user.role == "default"
      can :read, Store
      can :create, Store
    end
  end
end
  • 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-06-04T21:22:12+00:00Added an answer on June 4, 2026 at 9:22 pm

    Since a user will be able to create as many stores as they like, a store will belong to a user.

    You MUST create this relationship.

    So, in the User model.

    class User < ActiveRecord::Base
      has_many :stores
    end
    

    And in the Store model.

    class Store < ActiveRecord::Base
       belongs_to :user
    end
    

    And in the ability.rb file, just put something like:

    class Ability
      include CanCan::Ability
    
      def initialize(user)
        user ||= User.new # guest user (not logged in)
        if user.role == 'default'
          can :manage, Store , :user_id => user.id
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm actually using Ryan' Bates cancan gem authorization to make my application's users
I have created an application using Netbeans 6.9. In the application I want that
I have a web application using ASP.NET 2.0 and I want to know if
I'm using cancan in my rails 3 application with mysql DB. When i create
I have a Rails application using Rails 3. I added rspec-rails to my Gemfile
I have my application using Spring MVC + Apache 2 tiles + Spring security,
I have created application using TSQL and libraries. Right now I am generating clients
i have built application using codeigniter, my controller name is userReg , in controller
I am developing application using phonegap in eclipse for android .I have created folder
I am building web application using Spring MVC Spring Security Hibenate MySQl I want

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.