How to get a permission from a relationship using CanCanCan? The 2019 Stack Overflow Developer Survey Results Are In Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?declerative_authorization on User problemRails combobox issue with has_many :through relationshipHow to restrict user to search for a particular model in view?Rails, Devise, Role Model and CanCanCan - defining abilitiesCan't show “delete” for logged user by use !current_user?(user)Rails: cancancan gem condition is not workingRails 5 with CanCanCan - How to filter list for an index actionCanCanCan view only if has attribute

Why can't devices on different VLANs, but on the same subnet, communicate?

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Am I ethically obligated to go into work on an off day if the reason is sudden?

Make it rain characters

Can we generate random numbers using irrational numbers like π and e?

Is there a writing software that you can sort scenes like slides in PowerPoint?

What aspect of planet Earth must be changed to prevent the industrial revolution?

Why are there uneven bright areas in this photo of black hole?

First use of “packing” as in carrying a gun

What's the point in a preamp?

Why can't wing-mounted spoilers be used to steepen approaches?

should truth entail possible truth

Why not take a picture of a closer black hole?

Keeping a retro style to sci-fi spaceships?

Did the new image of black hole confirm the general theory of relativity?

How do I design a circuit to convert a 100 mV and 50 Hz sine wave to a square wave?

Was credit for the black hole image misappropriated?

What is the role of 'For' here?

What do I do when my TA workload is more than expected?

My body leaves; my core can stay

Sort list of array linked objects by keys and values

Student Loan from years ago pops up and is taking my salary

Is this wall load bearing? Blueprints and photos attached

Do working physicists consider Newtonian mechanics to be "falsified"?



How to get a permission from a relationship using CanCanCan?



The 2019 Stack Overflow Developer Survey Results Are In
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?declerative_authorization on User problemRails combobox issue with has_many :through relationshipHow to restrict user to search for a particular model in view?Rails, Devise, Role Model and CanCanCan - defining abilitiesCan't show “delete” for logged user by use !current_user?(user)Rails: cancancan gem condition is not workingRails 5 with CanCanCan - How to filter list for an index actionCanCanCan view only if has attribute



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















In my app I have a model called User that has_one Talent.



In CanCanCan I have this ability:



class Ability
include CanCan::Ability
def initialize(user)

if user.nil?
can :read, User
can :read, Talent, is_public?: true
else
can :read, Talent, is_public?: true
end


My page is being rendered by the ProfilesController#show. Like this:



class ProfilesController < ApplicationController
before_action :check_ability, except: [:show]
def show

@user = User.find(params[:id])
authorize! :read, @user
authorize! :read, @user.talent

if current_user
sent_connections = current_user.sent_connections
connections = sent_connections + current_user.all_connections
@is_connected = !(connections.select .empty?)
end
@top_5_photos = @user.top_5_photos
end


Well. Im trying to render a profile that the method: is_public returns false. But the page is being rendered correctly, while I expected was that the user cant see the page because of the rule:



 can :read, Talent, is_public?: true


What Im missing here?










share|improve this question
























  • What is the value of @user.talent.is_public?. Is it false?

    – Jay-Ar Polidario
    Mar 8 at 13:03











  • Yes. Even I putting a cannot :read, Talent. It doesnt work. Looks like something is overriding the permissions, but I triple-checked and nothing is setting the permissions on Talents.

    – Fernando Maymone
    Mar 8 at 13:05

















0















In my app I have a model called User that has_one Talent.



In CanCanCan I have this ability:



class Ability
include CanCan::Ability
def initialize(user)

if user.nil?
can :read, User
can :read, Talent, is_public?: true
else
can :read, Talent, is_public?: true
end


My page is being rendered by the ProfilesController#show. Like this:



class ProfilesController < ApplicationController
before_action :check_ability, except: [:show]
def show

@user = User.find(params[:id])
authorize! :read, @user
authorize! :read, @user.talent

if current_user
sent_connections = current_user.sent_connections
connections = sent_connections + current_user.all_connections
@is_connected = !(connections.select .empty?)
end
@top_5_photos = @user.top_5_photos
end


Well. Im trying to render a profile that the method: is_public returns false. But the page is being rendered correctly, while I expected was that the user cant see the page because of the rule:



 can :read, Talent, is_public?: true


What Im missing here?










share|improve this question
























  • What is the value of @user.talent.is_public?. Is it false?

    – Jay-Ar Polidario
    Mar 8 at 13:03











  • Yes. Even I putting a cannot :read, Talent. It doesnt work. Looks like something is overriding the permissions, but I triple-checked and nothing is setting the permissions on Talents.

    – Fernando Maymone
    Mar 8 at 13:05













0












0








0








In my app I have a model called User that has_one Talent.



In CanCanCan I have this ability:



class Ability
include CanCan::Ability
def initialize(user)

if user.nil?
can :read, User
can :read, Talent, is_public?: true
else
can :read, Talent, is_public?: true
end


My page is being rendered by the ProfilesController#show. Like this:



class ProfilesController < ApplicationController
before_action :check_ability, except: [:show]
def show

@user = User.find(params[:id])
authorize! :read, @user
authorize! :read, @user.talent

if current_user
sent_connections = current_user.sent_connections
connections = sent_connections + current_user.all_connections
@is_connected = !(connections.select .empty?)
end
@top_5_photos = @user.top_5_photos
end


Well. Im trying to render a profile that the method: is_public returns false. But the page is being rendered correctly, while I expected was that the user cant see the page because of the rule:



 can :read, Talent, is_public?: true


What Im missing here?










share|improve this question
















In my app I have a model called User that has_one Talent.



In CanCanCan I have this ability:



class Ability
include CanCan::Ability
def initialize(user)

if user.nil?
can :read, User
can :read, Talent, is_public?: true
else
can :read, Talent, is_public?: true
end


My page is being rendered by the ProfilesController#show. Like this:



class ProfilesController < ApplicationController
before_action :check_ability, except: [:show]
def show

@user = User.find(params[:id])
authorize! :read, @user
authorize! :read, @user.talent

if current_user
sent_connections = current_user.sent_connections
connections = sent_connections + current_user.all_connections
@is_connected = !(connections.select .empty?)
end
@top_5_photos = @user.top_5_photos
end


Well. Im trying to render a profile that the method: is_public returns false. But the page is being rendered correctly, while I expected was that the user cant see the page because of the rule:



 can :read, Talent, is_public?: true


What Im missing here?







ruby-on-rails cancancan






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 13:12







Fernando Maymone

















asked Mar 8 at 12:17









Fernando MaymoneFernando Maymone

1108




1108












  • What is the value of @user.talent.is_public?. Is it false?

    – Jay-Ar Polidario
    Mar 8 at 13:03











  • Yes. Even I putting a cannot :read, Talent. It doesnt work. Looks like something is overriding the permissions, but I triple-checked and nothing is setting the permissions on Talents.

    – Fernando Maymone
    Mar 8 at 13:05

















  • What is the value of @user.talent.is_public?. Is it false?

    – Jay-Ar Polidario
    Mar 8 at 13:03











  • Yes. Even I putting a cannot :read, Talent. It doesnt work. Looks like something is overriding the permissions, but I triple-checked and nothing is setting the permissions on Talents.

    – Fernando Maymone
    Mar 8 at 13:05
















What is the value of @user.talent.is_public?. Is it false?

– Jay-Ar Polidario
Mar 8 at 13:03





What is the value of @user.talent.is_public?. Is it false?

– Jay-Ar Polidario
Mar 8 at 13:03













Yes. Even I putting a cannot :read, Talent. It doesnt work. Looks like something is overriding the permissions, but I triple-checked and nothing is setting the permissions on Talents.

– Fernando Maymone
Mar 8 at 13:05





Yes. Even I putting a cannot :read, Talent. It doesnt work. Looks like something is overriding the permissions, but I triple-checked and nothing is setting the permissions on Talents.

– Fernando Maymone
Mar 8 at 13:05












1 Answer
1






active

oldest

votes


















2














If I remember it correctly,



can :read, Talent, is_public?: true


^ is_public? above is expected to be an attribute by Cancancan.



But because is_public? is a custom method, then can you try the following instead?



can :read, Talent do |talent|
talent.is_public?
end





share|improve this answer


















  • 1





    Hmm. Can you try adding cannot :manage, :all at the very end of your ability.rb's initialize method? Because this will override everything to be "not-authorised!". Then try again and you should hopefully not be able to see the page

    – Jay-Ar Polidario
    Mar 8 at 13:15






  • 1





    @FernandoMaymone, I just looked at it right now from the official docs, that you are supposed to just only use attributes there, and you should not use custom methods (but will still probably work, from my what I could remember, and is why yours still work; however I remember having some problems before with this, and was why I had to use the "block" form). See here: I quote: "It is important to only use database columns for these conditions so it can be used for Fetching Records."

    – Jay-Ar Polidario
    Mar 8 at 13:17







  • 1





    If cannot :manage, :all still allowed the rendering of the page, then maybe your "initialize" method was not actually being called, or that your show action is not getting called, or that for some reason you have a different ability object defined/overidding current_ability method somewhere, maybe in your application_controller

    – Jay-Ar Polidario
    Mar 8 at 13:21







  • 1





    hmm i see. I want to check something, can you try can :read, Talent do |talent|; puts 'XXXXXXX'; puts talent.is_public?; end. And then look at the running rails server, it should print out the value. Can you let me know the value being displayed? If it's false or true?

    – Jay-Ar Polidario
    Mar 8 at 13:25







  • 1





    @FernandoMaymone hmm it is looking to me like the show method is not being called. So, can you try adding puts 'MMMMMMMM' just above authorize! :read, @user.talent in your show method? and let me know if "MMMMM" gets printed out

    – Jay-Ar Polidario
    Mar 8 at 13:35











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063083%2fhow-to-get-a-permission-from-a-relationship-using-cancancan%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If I remember it correctly,



can :read, Talent, is_public?: true


^ is_public? above is expected to be an attribute by Cancancan.



But because is_public? is a custom method, then can you try the following instead?



can :read, Talent do |talent|
talent.is_public?
end





share|improve this answer


















  • 1





    Hmm. Can you try adding cannot :manage, :all at the very end of your ability.rb's initialize method? Because this will override everything to be "not-authorised!". Then try again and you should hopefully not be able to see the page

    – Jay-Ar Polidario
    Mar 8 at 13:15






  • 1





    @FernandoMaymone, I just looked at it right now from the official docs, that you are supposed to just only use attributes there, and you should not use custom methods (but will still probably work, from my what I could remember, and is why yours still work; however I remember having some problems before with this, and was why I had to use the "block" form). See here: I quote: "It is important to only use database columns for these conditions so it can be used for Fetching Records."

    – Jay-Ar Polidario
    Mar 8 at 13:17







  • 1





    If cannot :manage, :all still allowed the rendering of the page, then maybe your "initialize" method was not actually being called, or that your show action is not getting called, or that for some reason you have a different ability object defined/overidding current_ability method somewhere, maybe in your application_controller

    – Jay-Ar Polidario
    Mar 8 at 13:21







  • 1





    hmm i see. I want to check something, can you try can :read, Talent do |talent|; puts 'XXXXXXX'; puts talent.is_public?; end. And then look at the running rails server, it should print out the value. Can you let me know the value being displayed? If it's false or true?

    – Jay-Ar Polidario
    Mar 8 at 13:25







  • 1





    @FernandoMaymone hmm it is looking to me like the show method is not being called. So, can you try adding puts 'MMMMMMMM' just above authorize! :read, @user.talent in your show method? and let me know if "MMMMM" gets printed out

    – Jay-Ar Polidario
    Mar 8 at 13:35















2














If I remember it correctly,



can :read, Talent, is_public?: true


^ is_public? above is expected to be an attribute by Cancancan.



But because is_public? is a custom method, then can you try the following instead?



can :read, Talent do |talent|
talent.is_public?
end





share|improve this answer


















  • 1





    Hmm. Can you try adding cannot :manage, :all at the very end of your ability.rb's initialize method? Because this will override everything to be "not-authorised!". Then try again and you should hopefully not be able to see the page

    – Jay-Ar Polidario
    Mar 8 at 13:15






  • 1





    @FernandoMaymone, I just looked at it right now from the official docs, that you are supposed to just only use attributes there, and you should not use custom methods (but will still probably work, from my what I could remember, and is why yours still work; however I remember having some problems before with this, and was why I had to use the "block" form). See here: I quote: "It is important to only use database columns for these conditions so it can be used for Fetching Records."

    – Jay-Ar Polidario
    Mar 8 at 13:17







  • 1





    If cannot :manage, :all still allowed the rendering of the page, then maybe your "initialize" method was not actually being called, or that your show action is not getting called, or that for some reason you have a different ability object defined/overidding current_ability method somewhere, maybe in your application_controller

    – Jay-Ar Polidario
    Mar 8 at 13:21







  • 1





    hmm i see. I want to check something, can you try can :read, Talent do |talent|; puts 'XXXXXXX'; puts talent.is_public?; end. And then look at the running rails server, it should print out the value. Can you let me know the value being displayed? If it's false or true?

    – Jay-Ar Polidario
    Mar 8 at 13:25







  • 1





    @FernandoMaymone hmm it is looking to me like the show method is not being called. So, can you try adding puts 'MMMMMMMM' just above authorize! :read, @user.talent in your show method? and let me know if "MMMMM" gets printed out

    – Jay-Ar Polidario
    Mar 8 at 13:35













2












2








2







If I remember it correctly,



can :read, Talent, is_public?: true


^ is_public? above is expected to be an attribute by Cancancan.



But because is_public? is a custom method, then can you try the following instead?



can :read, Talent do |talent|
talent.is_public?
end





share|improve this answer













If I remember it correctly,



can :read, Talent, is_public?: true


^ is_public? above is expected to be an attribute by Cancancan.



But because is_public? is a custom method, then can you try the following instead?



can :read, Talent do |talent|
talent.is_public?
end






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 13:08









Jay-Ar PolidarioJay-Ar Polidario

4,866822




4,866822







  • 1





    Hmm. Can you try adding cannot :manage, :all at the very end of your ability.rb's initialize method? Because this will override everything to be "not-authorised!". Then try again and you should hopefully not be able to see the page

    – Jay-Ar Polidario
    Mar 8 at 13:15






  • 1





    @FernandoMaymone, I just looked at it right now from the official docs, that you are supposed to just only use attributes there, and you should not use custom methods (but will still probably work, from my what I could remember, and is why yours still work; however I remember having some problems before with this, and was why I had to use the "block" form). See here: I quote: "It is important to only use database columns for these conditions so it can be used for Fetching Records."

    – Jay-Ar Polidario
    Mar 8 at 13:17







  • 1





    If cannot :manage, :all still allowed the rendering of the page, then maybe your "initialize" method was not actually being called, or that your show action is not getting called, or that for some reason you have a different ability object defined/overidding current_ability method somewhere, maybe in your application_controller

    – Jay-Ar Polidario
    Mar 8 at 13:21







  • 1





    hmm i see. I want to check something, can you try can :read, Talent do |talent|; puts 'XXXXXXX'; puts talent.is_public?; end. And then look at the running rails server, it should print out the value. Can you let me know the value being displayed? If it's false or true?

    – Jay-Ar Polidario
    Mar 8 at 13:25







  • 1





    @FernandoMaymone hmm it is looking to me like the show method is not being called. So, can you try adding puts 'MMMMMMMM' just above authorize! :read, @user.talent in your show method? and let me know if "MMMMM" gets printed out

    – Jay-Ar Polidario
    Mar 8 at 13:35












  • 1





    Hmm. Can you try adding cannot :manage, :all at the very end of your ability.rb's initialize method? Because this will override everything to be "not-authorised!". Then try again and you should hopefully not be able to see the page

    – Jay-Ar Polidario
    Mar 8 at 13:15






  • 1





    @FernandoMaymone, I just looked at it right now from the official docs, that you are supposed to just only use attributes there, and you should not use custom methods (but will still probably work, from my what I could remember, and is why yours still work; however I remember having some problems before with this, and was why I had to use the "block" form). See here: I quote: "It is important to only use database columns for these conditions so it can be used for Fetching Records."

    – Jay-Ar Polidario
    Mar 8 at 13:17







  • 1





    If cannot :manage, :all still allowed the rendering of the page, then maybe your "initialize" method was not actually being called, or that your show action is not getting called, or that for some reason you have a different ability object defined/overidding current_ability method somewhere, maybe in your application_controller

    – Jay-Ar Polidario
    Mar 8 at 13:21







  • 1





    hmm i see. I want to check something, can you try can :read, Talent do |talent|; puts 'XXXXXXX'; puts talent.is_public?; end. And then look at the running rails server, it should print out the value. Can you let me know the value being displayed? If it's false or true?

    – Jay-Ar Polidario
    Mar 8 at 13:25







  • 1





    @FernandoMaymone hmm it is looking to me like the show method is not being called. So, can you try adding puts 'MMMMMMMM' just above authorize! :read, @user.talent in your show method? and let me know if "MMMMM" gets printed out

    – Jay-Ar Polidario
    Mar 8 at 13:35







1




1





Hmm. Can you try adding cannot :manage, :all at the very end of your ability.rb's initialize method? Because this will override everything to be "not-authorised!". Then try again and you should hopefully not be able to see the page

– Jay-Ar Polidario
Mar 8 at 13:15





Hmm. Can you try adding cannot :manage, :all at the very end of your ability.rb's initialize method? Because this will override everything to be "not-authorised!". Then try again and you should hopefully not be able to see the page

– Jay-Ar Polidario
Mar 8 at 13:15




1




1





@FernandoMaymone, I just looked at it right now from the official docs, that you are supposed to just only use attributes there, and you should not use custom methods (but will still probably work, from my what I could remember, and is why yours still work; however I remember having some problems before with this, and was why I had to use the "block" form). See here: I quote: "It is important to only use database columns for these conditions so it can be used for Fetching Records."

– Jay-Ar Polidario
Mar 8 at 13:17






@FernandoMaymone, I just looked at it right now from the official docs, that you are supposed to just only use attributes there, and you should not use custom methods (but will still probably work, from my what I could remember, and is why yours still work; however I remember having some problems before with this, and was why I had to use the "block" form). See here: I quote: "It is important to only use database columns for these conditions so it can be used for Fetching Records."

– Jay-Ar Polidario
Mar 8 at 13:17





1




1





If cannot :manage, :all still allowed the rendering of the page, then maybe your "initialize" method was not actually being called, or that your show action is not getting called, or that for some reason you have a different ability object defined/overidding current_ability method somewhere, maybe in your application_controller

– Jay-Ar Polidario
Mar 8 at 13:21






If cannot :manage, :all still allowed the rendering of the page, then maybe your "initialize" method was not actually being called, or that your show action is not getting called, or that for some reason you have a different ability object defined/overidding current_ability method somewhere, maybe in your application_controller

– Jay-Ar Polidario
Mar 8 at 13:21





1




1





hmm i see. I want to check something, can you try can :read, Talent do |talent|; puts 'XXXXXXX'; puts talent.is_public?; end. And then look at the running rails server, it should print out the value. Can you let me know the value being displayed? If it's false or true?

– Jay-Ar Polidario
Mar 8 at 13:25






hmm i see. I want to check something, can you try can :read, Talent do |talent|; puts 'XXXXXXX'; puts talent.is_public?; end. And then look at the running rails server, it should print out the value. Can you let me know the value being displayed? If it's false or true?

– Jay-Ar Polidario
Mar 8 at 13:25





1




1





@FernandoMaymone hmm it is looking to me like the show method is not being called. So, can you try adding puts 'MMMMMMMM' just above authorize! :read, @user.talent in your show method? and let me know if "MMMMM" gets printed out

– Jay-Ar Polidario
Mar 8 at 13:35





@FernandoMaymone hmm it is looking to me like the show method is not being called. So, can you try adding puts 'MMMMMMMM' just above authorize! :read, @user.talent in your show method? and let me know if "MMMMM" gets printed out

– Jay-Ar Polidario
Mar 8 at 13:35



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063083%2fhow-to-get-a-permission-from-a-relationship-using-cancancan%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко