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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:48:43+00:00 2026-05-25T15:48:43+00:00

I have the following: User has_many Listings Listing has_many Offers Pretty basic. A user

  • 0

I have the following:

User has_many Listings
Listing has_many Offers

Pretty basic. A user can publish listings and other users can make offers on those listings.

On each Listing View, you can see a description of the listing, and also a list of all of the offers it has received. Also, on this same view, there is a form which lets users make an offer on this Listing.

Of course, if I am looking at my own Listing, I do not want to be able to make an Offer. So, I’m trying to add a restriction on the Offer’s create method, to only allow access to it if it is from a User different from the one that posted this Listing. In my Ability class, it would be something like:

can :create, Offer if listing.user != user

This doesn’t work since listing is not defined anywhere. This method will be called when a user is viewing a certain listing, so, how can I pass this current Listing to the cancan method to effectively check this restriction?

Thanks.

  • 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-25T15:48:44+00:00Added an answer on May 25, 2026 at 3:48 pm

    Move the CanCan ability check until after the point at which you’ve created the new offer. E.g.

    #listing.rb
    load_and_authorize_resource :except => :create
    
    def create
        @offer = #whatever it is you want to do
        authorize! :create, @offer
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: User and Car with the following associations: User has_many Car
If I have the following has_one setup: class Account has_one :user How can I
I have the following models: class User < ActiveRecord::Base has_many :subscriptions, as: :subscribable has_many
In my Rails app, I have the following objects: Group: has_many users through group_membership
I am working with Rails 2.3 and I have the following associations: User: has_many
I have the following setup of models: User - has_many File (for userpics) Gallery
I have the following associations in my application: # user.rb has_many :posts, :dependent =>
I have the following relationship User --> has_many: Articles Article --> belongs_to: User I
I have the following relationships: user.rb has_many :ownerships has_many :restaurants, :through => :ownerships restaurant.rb
I have following models: User has_many :roles, :through => :assignments Role has_many :assignments has_many

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.