Get current_user id in model2019 Community Moderator ElectionAccess current_user in modelAccess to current_user from within a model in Ruby on RailsAccess Devise's current_user in ModelHow do I not use current_user in the modelgetting current_user in any model in railsHow to use concerns in Rails 4rails model before_update can't access current_userRails model behavior involving current_usercurrent_user not working in User model. I'm using DeviseHow to get current_user in Rails 5 API with DeviseHow to save current_user email and show it to the user?

Should we avoid writing fiction about historical events without extensive research?

A bug in Excel? Conditional formatting for marking duplicates also highlights unique value

Are small insurances worth it

Can a space-faring robot still function over a billion years?

Why aren't there more gauls like Obelix?

If nine coins are tossed, what is the probability that the number of heads is even?

Replacing tantalum capacitor with ceramic capacitor for Op Amps

What is the oldest European royal house?

Integrating function with /; in its definition

Remove object from array based on array of some property of that object

Ultrafilters as a double dual

Why would the IRS ask for birth certificates or even audit a small tax return?

An Undercover Army

Error in TransformedField

Dukha vs legitimate need

What is better: yes / no radio, or simple checkbox?

Paper published similar to PhD thesis

Learning to quickly identify valid fingering for piano?

Can verbs simutaniously use the volitional form and negative form?

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Why can't we use freedom of speech and expression to incite people to rebel against government in India?

Giving a talk in my old university, how prominently should I tell students my salary?

Short story about an infectious indestructible metal bar?

What is Tony Stark injecting into himself in Iron Man 3?



Get current_user id in model



2019 Community Moderator ElectionAccess current_user in modelAccess to current_user from within a model in Ruby on RailsAccess Devise's current_user in ModelHow do I not use current_user in the modelgetting current_user in any model in railsHow to use concerns in Rails 4rails model before_update can't access current_userRails model behavior involving current_usercurrent_user not working in User model. I'm using DeviseHow to get current_user in Rails 5 API with DeviseHow to save current_user email and show it to the user?










0















How to get the current user id in model like post.



 def read?
if self.users.ids.include? @current_user
'read'
else
'unread'
end
end


If i am trying to use as @current user it not takes. Even passing through controller?










share|improve this question






















  • What's your goal to know id of current user in model?

    – mechnicov
    yesterday











  • Why not passing the id as argument? read?(current_user.id) ?

    – Зелёный
    yesterday











  • @mechnicov to check if he visit the post page or not

    – Rajkumar
    yesterday











  • It's out of model logic

    – mechnicov
    yesterday






  • 1





    Possible duplicate of Access current_user in model

    – ray
    yesterday















0















How to get the current user id in model like post.



 def read?
if self.users.ids.include? @current_user
'read'
else
'unread'
end
end


If i am trying to use as @current user it not takes. Even passing through controller?










share|improve this question






















  • What's your goal to know id of current user in model?

    – mechnicov
    yesterday











  • Why not passing the id as argument? read?(current_user.id) ?

    – Зелёный
    yesterday











  • @mechnicov to check if he visit the post page or not

    – Rajkumar
    yesterday











  • It's out of model logic

    – mechnicov
    yesterday






  • 1





    Possible duplicate of Access current_user in model

    – ray
    yesterday













0












0








0








How to get the current user id in model like post.



 def read?
if self.users.ids.include? @current_user
'read'
else
'unread'
end
end


If i am trying to use as @current user it not takes. Even passing through controller?










share|improve this question














How to get the current user id in model like post.



 def read?
if self.users.ids.include? @current_user
'read'
else
'unread'
end
end


If i am trying to use as @current user it not takes. Even passing through controller?







ruby-on-rails devise






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









RajkumarRajkumar

314




314












  • What's your goal to know id of current user in model?

    – mechnicov
    yesterday











  • Why not passing the id as argument? read?(current_user.id) ?

    – Зелёный
    yesterday











  • @mechnicov to check if he visit the post page or not

    – Rajkumar
    yesterday











  • It's out of model logic

    – mechnicov
    yesterday






  • 1





    Possible duplicate of Access current_user in model

    – ray
    yesterday

















  • What's your goal to know id of current user in model?

    – mechnicov
    yesterday











  • Why not passing the id as argument? read?(current_user.id) ?

    – Зелёный
    yesterday











  • @mechnicov to check if he visit the post page or not

    – Rajkumar
    yesterday











  • It's out of model logic

    – mechnicov
    yesterday






  • 1





    Possible duplicate of Access current_user in model

    – ray
    yesterday
















What's your goal to know id of current user in model?

– mechnicov
yesterday





What's your goal to know id of current user in model?

– mechnicov
yesterday













Why not passing the id as argument? read?(current_user.id) ?

– Зелёный
yesterday





Why not passing the id as argument? read?(current_user.id) ?

– Зелёный
yesterday













@mechnicov to check if he visit the post page or not

– Rajkumar
yesterday





@mechnicov to check if he visit the post page or not

– Rajkumar
yesterday













It's out of model logic

– mechnicov
yesterday





It's out of model logic

– mechnicov
yesterday




1




1





Possible duplicate of Access current_user in model

– ray
yesterday





Possible duplicate of Access current_user in model

– ray
yesterday












2 Answers
2






active

oldest

votes


















0














It is not possible to get current_user in the model unless you pass it from the controller.






share|improve this answer






























    0














    Helpers method are not directly accessible inside model, to access it you would need to explicitly call it it like this -



    current_user = ApplicationController.helpers.current_user


    But i would suggest you to pass current_user as an argument in method like this -



    class MyModel < ApplicationRecord
    #current_user = ApplicationController.helpers.current_user

    def read?(user)
    return self.users.ids.include?(user) ? true : false
    end
    end


    So that it would be convenient way to use this method



    obj = MyModel.first
    obj.read?(current_user)





    share|improve this answer
























      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%2f55022240%2fget-current-user-id-in-model%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      It is not possible to get current_user in the model unless you pass it from the controller.






      share|improve this answer



























        0














        It is not possible to get current_user in the model unless you pass it from the controller.






        share|improve this answer

























          0












          0








          0







          It is not possible to get current_user in the model unless you pass it from the controller.






          share|improve this answer













          It is not possible to get current_user in the model unless you pass it from the controller.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Tashi DendupTashi Dendup

          216




          216























              0














              Helpers method are not directly accessible inside model, to access it you would need to explicitly call it it like this -



              current_user = ApplicationController.helpers.current_user


              But i would suggest you to pass current_user as an argument in method like this -



              class MyModel < ApplicationRecord
              #current_user = ApplicationController.helpers.current_user

              def read?(user)
              return self.users.ids.include?(user) ? true : false
              end
              end


              So that it would be convenient way to use this method



              obj = MyModel.first
              obj.read?(current_user)





              share|improve this answer





























                0














                Helpers method are not directly accessible inside model, to access it you would need to explicitly call it it like this -



                current_user = ApplicationController.helpers.current_user


                But i would suggest you to pass current_user as an argument in method like this -



                class MyModel < ApplicationRecord
                #current_user = ApplicationController.helpers.current_user

                def read?(user)
                return self.users.ids.include?(user) ? true : false
                end
                end


                So that it would be convenient way to use this method



                obj = MyModel.first
                obj.read?(current_user)





                share|improve this answer



























                  0












                  0








                  0







                  Helpers method are not directly accessible inside model, to access it you would need to explicitly call it it like this -



                  current_user = ApplicationController.helpers.current_user


                  But i would suggest you to pass current_user as an argument in method like this -



                  class MyModel < ApplicationRecord
                  #current_user = ApplicationController.helpers.current_user

                  def read?(user)
                  return self.users.ids.include?(user) ? true : false
                  end
                  end


                  So that it would be convenient way to use this method



                  obj = MyModel.first
                  obj.read?(current_user)





                  share|improve this answer















                  Helpers method are not directly accessible inside model, to access it you would need to explicitly call it it like this -



                  current_user = ApplicationController.helpers.current_user


                  But i would suggest you to pass current_user as an argument in method like this -



                  class MyModel < ApplicationRecord
                  #current_user = ApplicationController.helpers.current_user

                  def read?(user)
                  return self.users.ids.include?(user) ? true : false
                  end
                  end


                  So that it would be convenient way to use this method



                  obj = MyModel.first
                  obj.read?(current_user)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  GabbarGabbar

                  4,4402418




                  4,4402418



























                      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%2f55022240%2fget-current-user-id-in-model%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 у кіно

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

                      Ель Греко