First, I do simple squeel testing in the Rails console.
User.where{name == "abc"}
There is no problem for new-created Ruby project
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'abc' => []
However, when I apply it on my existing project, it gives me error message:
1.9.3p125 :001 > User.where{name == "abc"}
ArgumentError: wrong number of arguments (0 for 1) from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/parse_resource-1.7.3/lib/parse_resource/query.rb:11:in `where'
from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/parse_resource-1.7.3/lib/parse_resource/base.rb:243:in `where'
from (irb):1
from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/xxx/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
The most-related issue I found is Squeel issue #193.
It says that Squeel is not loaded. But, in my case, new-created can be implemented in the same machine.
Anyway, I try to change active_record.rb as it stated, but the problem is still here for my existing project.
I'm using Ruby-1.9.3-p125, Rails v3.2.6, and Squeel 1.0.14.
Well instead of using where clause you can use:
User.filter(:name => 'abc').first
This might work in your case
Related
I am stumped and would appreciate some help.
Using devise 4.2.1 and rails 5.0. Successfully set up devise model and create a resource. Yet, when I attempt to query the admin table in rails console, I get this error:
2.4.0 :003 > User.all
User Load (0.5ms) SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
2.4.0 :004 > Admin.all
Admin Load (1.4ms) SELECT "admins".* FROM "admins"
ArgumentError: wrong number of arguments (given 0, expected 1)
from /Users/nonni/.rvm/gems/ruby-2.4.0#spree/gems/devise-4.2.1/lib/devise/models/database_authenticatable.rb:157:in `password_digest'
from /Users/nonni/.rvm/gems/ruby-2.4.0#spree/gems/activemodel-5.0.2/lib/active_model/serialization.rb:135:in `block in serializable_hash'
from /Users/nonni/.rvm/gems/ruby-2.4.0#spree/gems/activemodel-5.0.2/lib/active_model/serialization.rb:135:in `each'
from /Users/nonni/.rvm/gems/ruby-2.4.0#spree/gems/activemodel-
from /Users/nonni/.rvm/gems/ruby-2.4.0#spree/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
This line in particular seems to be where the error occurs:
4.2.1/lib/devise/models/database_authenticatable.rb:157:in `password_digest' from /Users/nonni/.rvm/gems/ruby 2.4.0#spree/gems/activemodel-
When I inspect postico, I see the newly created record:
2 rand#user.com $2a$11$X9IcCMq9rSIz/QzTmoXfi.Se1nximWq8rRjAClpqJHgbPArJbqGAW1 2017-04-18 2017-04-18 (removed other_time_steps)
There isn't a password_digest column on Admins table (devise generates an encryted_password column as long as it receives password as a named argument).
I also get this same error when I attempt to create a new admin in console. What could I possibly be missing? Would appreciate any pointers. Thanks!
I'm able to do the bare bones keyword search with no issues in my app using elasticsearch, but as_indexed_json isn't working apparently and I have no idea why. I'm following this tutorial, and the section in question is almost halfway down.
Please let me know if I need to provide any additional info. I'm pretty new to this, so I apologize if this is a really dumb question.
Rails console:
2.2.4 :011 > Term.first.as_indexed_json
Term Load (0.4ms) SELECT "terms".* FROM "terms" ORDER BY "terms"."id" ASC LIMIT 1
NoMethodError: undefined method `as_indexed_json' for #<Term:0x007fa6cd9a6408>
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.5/lib/active_model/attribute_methods.rb:433:in `method_missing'
from (irb):11
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Model:
require 'elasticsearch/model'
class Term < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def as_indexed_json(options = {})
as_json(
only: [:name, :definition, :etymology1, :etymology2, :uses, :romance_cognates, :notes1, :notes2, :quote1, :quote2]
)
end
end
Based on that tutorial i hope you opened the irb using rails c.
But open console like this bundle exec rails c and try again
I have been working in many Rails applications and during the period I have found many bugs but one thing really pissed me off is the error showing the Ruby file name in the terminal.
I tried to run
User.find(3)
in the Rails console, and, though I don't have more than one user record, it was obviously showing an error and I was aware of it. But the location part really messed up my terminal.
Here is the error log:
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=3
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activerecord-4.2.0.beta2/lib/active_record/core.rb:150:in `find'
from (irb):79
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /home/travis/tumpy/bin/rails:8:in `<top (required)>'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /home/travis/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /home/travis/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/travis/.rvm/rubies/ruby-
The solution I was expecting is to hide the location part and only display the error, showing
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=3
IRB has a back_trace_limit option which defaults to 16:
irb(main):001:0> IRB.CurrentContext.back_trace_limit
#=> 16
Setting it to 0 suppresses the entire backtrace:
irb(main):002:0> IRB.CurrentContext.back_trace_limit = 0
#=> 0
irb(main):003:0> User.find(0)
# User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 0 LIMIT 1
#ActiveRecord::RecordNotFound: Couldn't find User with id=0
irb(main):004:0>
You can also pass this as a command line option:
$ rails console -- --back-trace-limit=0
I'm using find_all_by to get the returned array from active record, and this feature works well for Rails(4.0.0)
Loading production environment (Rails 4.0.0)
2.0.0-p353 :001 > Subscription.find_all_by_id 123
=> []
But when I changed it to Rails(4.1.5), find_all_by doesn't work, and throw undefined method error
Loading production environment (Rails 4.1.5)
2.0.0-p481 :001 > Subscription.find_all_by_id 123
NoMethodError: undefined method `find_all_by_id' for #<Class:0x00000004d71b68>
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.5/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
from (irb):1
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
Any ideas to fix it or work around it?
How about this?
Subscription.where(id: "123").to_a
For anyone else who has been trying to get find_all_by to work and have had no success, find_all_by has been deprecated and removed from rails.
Source: https://github.com/rails/rails/issues/17357
I'm using Rails 3.2.12 and Roby 2.0.0-p0 in RVM etc. In rails console I get the following error after doing u = Users.first:
u = Users.first
Loading production environment (Rails 3.2.12)
2.0.0p0 :001 > u = Users.first
NoMethodError: undefined method `first' for Users:Module
from (irb):1
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I was able to access the database and view the record via the psql console using the user rails would be using.
If you already have a User model defined, try User.first instead of Users.first.