Ruby on Rails - field not recognized by ActiveRecord relation Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to get a random number in RubyA concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?Ruby on Rails Server optionsPurge or recreate a Ruby on Rails databaseRails 3, Action Mailer, attachments, form_for, emailupdate_attributes failing when I try to update an attributeSelect from both tables with where clause with Rails 5

Maximum summed subsequences with non-adjacent items

How to run automated tests after each commit?

How long can equipment go unused before powering up runs the risk of damage?

Why can't I install Tomboy in Ubuntu Mate 19.04?

How does light 'choose' between wave and particle behaviour?

AppleTVs create a chatty alternate WiFi network

Amount of permutations on an NxNxN Rubik's Cube

What is the meaning of 'breadth' in breadth first search?

What does it mean that physics no longer uses mechanical models to describe phenomena?

Girl Hackers - Logic Puzzle

Random body shuffle every night—can we still function?

If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?

In musical terms, what properties are varied by the human voice to produce different words / syllables?

How often does castling occur in grandmaster games?

Drawing spherical mirrors

How to compare two different files line by line in unix?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Tannaka duality for semisimple groups

What initially awakened the Balrog?

What's the point of the test set?

Do wooden building fires get hotter than 600°C?

Can the Flaming Sphere spell be rammed into multiple Tiny creatures that are in the same 5-foot square?

Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?

Do I really need to have a message in a novel to appeal to readers?



Ruby on Rails - field not recognized by ActiveRecord relation



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to get a random number in RubyA concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?Ruby on Rails Server optionsPurge or recreate a Ruby on Rails databaseRails 3, Action Mailer, attachments, form_for, emailupdate_attributes failing when I try to update an attributeSelect from both tables with where clause with Rails 5



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








0















I have a User table for my app which contains the list of all users. This table has a Boolean field named active.



I have this code to fetch the user:



existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase)


And this is what I get when I do an existing_user.inspect:



User Load (1.9ms) SELECT "users".* FROM "users" WHERE (LOWER(email) = 'biswanath.chandramouli@gmail.com')
#<ActiveRecord::Relation [#<User id: 4, name: "Biswanath Chandramouli", email: "Biswanath.Chandramouli@gmail.com", last_updated_by: "", admin: true, active: true, last_updated_on: nil, created_at: "2018-10-30 08:14:59", updated_at: "2018-10-30 08:14:59"


As you can see, existing_user has the property active available as shown above.



But this code fails:



if(!existing_user.active?)


The above call throws this error:



undefined method `active?' for #<User::ActiveRecord_Relation:0x00007f0a58b2c500> Did you mean? acts_like?


When existing_user.inspect shows active: true, why does the above call existing_user.active fail? Pls help!










share|improve this question




























    0















    I have a User table for my app which contains the list of all users. This table has a Boolean field named active.



    I have this code to fetch the user:



    existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase)


    And this is what I get when I do an existing_user.inspect:



    User Load (1.9ms) SELECT "users".* FROM "users" WHERE (LOWER(email) = 'biswanath.chandramouli@gmail.com')
    #<ActiveRecord::Relation [#<User id: 4, name: "Biswanath Chandramouli", email: "Biswanath.Chandramouli@gmail.com", last_updated_by: "", admin: true, active: true, last_updated_on: nil, created_at: "2018-10-30 08:14:59", updated_at: "2018-10-30 08:14:59"


    As you can see, existing_user has the property active available as shown above.



    But this code fails:



    if(!existing_user.active?)


    The above call throws this error:



    undefined method `active?' for #<User::ActiveRecord_Relation:0x00007f0a58b2c500> Did you mean? acts_like?


    When existing_user.inspect shows active: true, why does the above call existing_user.active fail? Pls help!










    share|improve this question
























      0












      0








      0








      I have a User table for my app which contains the list of all users. This table has a Boolean field named active.



      I have this code to fetch the user:



      existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase)


      And this is what I get when I do an existing_user.inspect:



      User Load (1.9ms) SELECT "users".* FROM "users" WHERE (LOWER(email) = 'biswanath.chandramouli@gmail.com')
      #<ActiveRecord::Relation [#<User id: 4, name: "Biswanath Chandramouli", email: "Biswanath.Chandramouli@gmail.com", last_updated_by: "", admin: true, active: true, last_updated_on: nil, created_at: "2018-10-30 08:14:59", updated_at: "2018-10-30 08:14:59"


      As you can see, existing_user has the property active available as shown above.



      But this code fails:



      if(!existing_user.active?)


      The above call throws this error:



      undefined method `active?' for #<User::ActiveRecord_Relation:0x00007f0a58b2c500> Did you mean? acts_like?


      When existing_user.inspect shows active: true, why does the above call existing_user.active fail? Pls help!










      share|improve this question














      I have a User table for my app which contains the list of all users. This table has a Boolean field named active.



      I have this code to fetch the user:



      existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase)


      And this is what I get when I do an existing_user.inspect:



      User Load (1.9ms) SELECT "users".* FROM "users" WHERE (LOWER(email) = 'biswanath.chandramouli@gmail.com')
      #<ActiveRecord::Relation [#<User id: 4, name: "Biswanath Chandramouli", email: "Biswanath.Chandramouli@gmail.com", last_updated_by: "", admin: true, active: true, last_updated_on: nil, created_at: "2018-10-30 08:14:59", updated_at: "2018-10-30 08:14:59"


      As you can see, existing_user has the property active available as shown above.



      But this code fails:



      if(!existing_user.active?)


      The above call throws this error:



      undefined method `active?' for #<User::ActiveRecord_Relation:0x00007f0a58b2c500> Did you mean? acts_like?


      When existing_user.inspect shows active: true, why does the above call existing_user.active fail? Pls help!







      ruby-on-rails






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 10:02









      m.beginnerm.beginner

      448




      448






















          2 Answers
          2






          active

          oldest

          votes


















          5














          I think you should use if(!existing_user.first.active?). This will work in your case. Where clause returns you an array, not an object. In your case, existing_user is an array not an object.






          share|improve this answer
































            0














            This answer is off-topic but could save you a lot:



            Every time you call this existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase), it's going to downcase all the emails in the table and look for the correct one.



            I would suggest to downcase the email before saving the user and add an index on it;



            before_save self.email = self.email.downcase 


            and then get the user:



            user = User.where(email: auth_hash['info']['email'].downcase).first


            Try this method and you'll see a big difference in the data retrieval (which is now 1.9ms)






            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%2f55297149%2fruby-on-rails-field-not-recognized-by-activerecord-relation%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









              5














              I think you should use if(!existing_user.first.active?). This will work in your case. Where clause returns you an array, not an object. In your case, existing_user is an array not an object.






              share|improve this answer





























                5














                I think you should use if(!existing_user.first.active?). This will work in your case. Where clause returns you an array, not an object. In your case, existing_user is an array not an object.






                share|improve this answer



























                  5












                  5








                  5







                  I think you should use if(!existing_user.first.active?). This will work in your case. Where clause returns you an array, not an object. In your case, existing_user is an array not an object.






                  share|improve this answer















                  I think you should use if(!existing_user.first.active?). This will work in your case. Where clause returns you an array, not an object. In your case, existing_user is an array not an object.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 22 at 10:13









                  Umar Khan

                  1,020715




                  1,020715










                  answered Mar 22 at 10:08









                  NN796NN796

                  347522




                  347522























                      0














                      This answer is off-topic but could save you a lot:



                      Every time you call this existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase), it's going to downcase all the emails in the table and look for the correct one.



                      I would suggest to downcase the email before saving the user and add an index on it;



                      before_save self.email = self.email.downcase 


                      and then get the user:



                      user = User.where(email: auth_hash['info']['email'].downcase).first


                      Try this method and you'll see a big difference in the data retrieval (which is now 1.9ms)






                      share|improve this answer



























                        0














                        This answer is off-topic but could save you a lot:



                        Every time you call this existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase), it's going to downcase all the emails in the table and look for the correct one.



                        I would suggest to downcase the email before saving the user and add an index on it;



                        before_save self.email = self.email.downcase 


                        and then get the user:



                        user = User.where(email: auth_hash['info']['email'].downcase).first


                        Try this method and you'll see a big difference in the data retrieval (which is now 1.9ms)






                        share|improve this answer

























                          0












                          0








                          0







                          This answer is off-topic but could save you a lot:



                          Every time you call this existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase), it's going to downcase all the emails in the table and look for the correct one.



                          I would suggest to downcase the email before saving the user and add an index on it;



                          before_save self.email = self.email.downcase 


                          and then get the user:



                          user = User.where(email: auth_hash['info']['email'].downcase).first


                          Try this method and you'll see a big difference in the data retrieval (which is now 1.9ms)






                          share|improve this answer













                          This answer is off-topic but could save you a lot:



                          Every time you call this existing_user = User.where("LOWER(email) = ?", auth_hash['info']['email'].downcase), it's going to downcase all the emails in the table and look for the correct one.



                          I would suggest to downcase the email before saving the user and add an index on it;



                          before_save self.email = self.email.downcase 


                          and then get the user:



                          user = User.where(email: auth_hash['info']['email'].downcase).first


                          Try this method and you'll see a big difference in the data retrieval (which is now 1.9ms)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 22 at 11:01









                          Roc KhalilRoc Khalil

                          375114




                          375114



























                              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%2f55297149%2fruby-on-rails-field-not-recognized-by-activerecord-relation%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

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript