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;
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
add a comment |
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
add a comment |
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
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
ruby-on-rails
asked Mar 22 at 10:02
m.beginnerm.beginner
448
448
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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
)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 22 at 10:13
Umar Khan
1,020715
1,020715
answered Mar 22 at 10:08
NN796NN796
347522
347522
add a comment |
add a comment |
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
)
add a comment |
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
)
add a comment |
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
)
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
)
answered Mar 22 at 11:01
Roc KhalilRoc Khalil
375114
375114
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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