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!
Related
I'm trying to create an Active Record anonymous association extension has the documentation explains here. And I've got this models
class Program < ActiveRecord::Base
has_many :reports do
def asdf
"Hello extension"
end
end
end
class Report < ActiveRecord::Base
belongs_to :program
end
But when I test this on my rails c console I've an undefined method error.
Loading development environment (Rails 4.2.5)
2.1.0 :001 > Program.first.reports.asdf
Program Load (0.3ms) SELECT `programs`.* FROM `programs` ORDER BY `programs`.`id` ASC LIMIT 1
NoMethodError: Report Load (0.3ms) SELECT `reports`.* FROM `reports` WHERE `reports`.`program_id` = 1
undefined method `asdf' for #<Report::ActiveRecord_Associations_CollectionProxy:0x000001035e0098>
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:136:in `method_missing'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/activerecord-4.2.5/lib/active_record/relation/delegation.rb:99:in `method_missing'
from (irb):1
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/mariowise/.rvm/gems/ruby-2.1.0/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>'
I'm not trying to share the same extension between many associations, just trying to make this work through the anonymous module (block).
Any ideas?
That's because you retrieved many reports and called the method asdf on them, whereas your method asdf is defined for a single report.
Try this
Program.first.reports.first.asdf
I have a question with Ruby on Rails using:
rails console --sandbox
So in Michael Hartl's rails tutorial 3rd edition we are using this and I have a problem when sometimes I'm using it and it all goes well and then I have an error, and literally the whole test environment becomes useless in that it will not respond to my commands
For example, I will type:
2.1.1 :025 > user.first
or any other command that was working and should be working and instead get:
NoMethodError: undefined method `first' for #<User:0x007fede35e3188>
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activemodel-4.2.0.beta2/lib/active_model/attribute_methods.rb:435:in `method_missing' from (irb):25
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/workspace2/sample_app/bin/rails:8:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1#railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/andrewkim/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/andrewkim/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.1.1 :026 >
All of a sudden anything I type in terminal will come with an error like this, and I don't know how to go back without exiting "rails console --sandbox". is there a way?
for example, I will type:
2.1.1 :025 > user.first
or any other command that was working and should be working
That command shouldn't work, and it doesn't work. You are getting an error because in the tutorial a User instance, which represents one row in your table, e.g.
user = User.new(name: "Joe", email: "joe#yahoo.com")
does not have a method named first(). Instead, a user instance has the methods:
id()
name()
email()
created_at()
updated_at()
However, the class name(or model name) can be used to search the table, e.g.
user1 = User.first. #User is the name of the class/model
As for this:
and I don't know how to go back
Go back to what? Nothing has changed. All the variables you typed into the console before that error will still exist. Here is an example from the rails console:
$ rails console --sandbox
Loading development environment in sandbox (Rails 4.0.8)
Any modifications you make will be rolled back on exit
2.0.0-p481 :001 > x = 10
=> 10
2.0.0-p481 :002 > user = User.find_by(email: "example#railstutorial.org")
User Load (4.0ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'example#railstutorial.org' LIMIT 1
=> #<User id: 2, name: "Example User", email: "example#railstutorial.org", created_at: "2014-10-02 01:39:04", updated_at: "2014-10-02 01:39:04", password_digest: "$2a$10$VNzivk5opu1AC5DOM467dO2JaTg3c3JC9OAXR0AqvEi0...">
2.0.0-p481 :003 > user.first
NoMethodError: undefined method `first' for #<User:0x00000100f360c8>
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/activemodel-4.0.8/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/activerecord-4.0.8/lib/active_record/attribute_methods.rb:168:in `method_missing'
from (irb):3
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:90:in `start'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:9:in `start'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481#sample_app2_gems/gems/railties-4.0.8/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0-p481 :004 > user
=> #<User id: 2, name: "Example User", email: "example#railstutorial.org", created_at: "2014-10-02 01:39:04", updated_at: "2014-10-02 01:39:04", password_digest: "$2a$10$VNzivk5opu1AC5DOM467dO2JaTg3c3JC9OAXR0AqvEi0...">
2.0.0-p481 :005 > x
=> 10
2.0.0-p481 :006 >
See? Everything is still there. On the other hand, if by "go back" you mean, "go back to the blissful state you were in when you got no errors", then the solution is to type the correct commands into the console. Next time, copy the command from the tutorial that you are trying to emulate and paste it into a blank text file, then copy the command that is giving you an error(but which you know is correct!) and paste it under the command from the tutorial:
User.first
user.first
Then compare them. Your problem can also be mitigated somewhat by never creating a variable that has the same name as your model, e.g. instead of writing:
user = User.new(...)
write:
my_user = User.new(...)
Then if you write:
user.first
You will get the error:
NameError: undefined local variable or method `user' for main:Object
which should be easier for you to debug.
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.
We are trying to use send strftime('%Y/%m/%d') with a date object in our rails 3.2.12 app. Here is a test we did in rails console:
irb(main):014:0> b=User.offset(1).first
←[1m←[35mUser Load (1.0ms)←[0m SELECT "users".* FROM "users" LIMIT 1 OFFSET 1
=> #<User id: 2, login: "wsxl207", name: "?????", email: nil, encrypted_password: "55a48ac66ba164805d91dc5415455cd402e9c8b3a656620136a...", salt: "3ee
e529200bdcad3ed2f16a8749ede6f046130e2705714ab55d...", status: "active", user_type: "employee", input_by_id: 1, created_at: "2012-04-13 16:23:25", upda
ted_at: "2012-08-22 03:15:12">
irb(main):015:0> b.send('created_at').send("strftime('%Y/%m/%d')")
NoMethodError: undefined method `strftime('%Y/%m/%d')' for 2012-04-13 16:23:25 UTC:Time
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.12/lib/active_support/time_with_zone.rb:332:in `method_missing'
from (irb):15
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from C:/Ruby193/lib/ruby/gems/1.9.1/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>'
irb(main):016:0> b.send('created_at').strftime('%Y/%m/%d')
=> "2012/04/13"
However we found out that send does not work with strftime('%Y/%m/%d') with send("strftime('%Y/%m/%d')"). What we are missing here? Thanks for the help.
send take a method name (ideally as a symbol, though a string works, too) as its first arguments, and arguments for the method call as the remaining arguments. So I suppose what you're looking for is:
b.send(:created_at).send(:strftime, "%Y/%m/%d")
That said, if you're doing this explicitly in your code, you almost certainly want to just call created_at and strftime directly, rather than using the metaprogramming method send.
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