How to send(“strftime('%Y/%m/%d')”) in rails 3.2.12? - ruby-on-rails

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.

Related

[console in rails]. uninitialized constant Class

when i run a method in terminal of rails.
first time, it working :
Spree::Campaign.first
Campaign Load (0.4ms) SELECT `campaigns`.* FROM `campaigns` ORDER BY `campaigns`.`id` ASC LIMIT 1
=> #<Campaign id: 1, name: "campaign 1", user_id: 1, created_at: "2015-10-27 06:48:01", updated_at: "2015-10-29 04:22:03", description: nil, active: true>
but when i try run code above again
Spree::Campaign.first
NameError: uninitialized constant Spree::Campaign
from (irb):2
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/console.rb:90:in `start'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/console.rb:9:in `start'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/railties-4.1.6/lib/rails/commands.rb:17:in `<top (required)>'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
from /home/kop/.rvm/gems/ruby-2.1.4#rails3213/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
from /home/kop/rails/donghoxteen/bin/rails:8:in `<top (required)>'
from /home/kop/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/kop/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
Why? and how to fix this error??
The errors you are getting seem to be intermittent. In your question text, the error is uninitialized constant Spree::Campaign, whereas in your question title, the error is uninitialized constant Class.
This points to there being something fundamentally wrong with your Ruby installation. Class is a core Ruby class, and the Class constant should always be there. It's hard to diagnose with just the information you have given, but it appears that constant lookup is broken in your Ruby installation.
I recommend re-installing Ruby.

rails console --sandbox michael hartl's rails tutorial 3rd edition not sure how this works

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.

NameError: undefined local variable or method `user' for main:Object

I'm following through the Ruby on Rails tutorial, and when attempting to save a user in Rails Console (sandbox mode), I get the following error:
NameError: undefined local variable or method `user' for main:Object
from (irb):7
Note: I typed in User.new, input a name and email, then user.save, and got the error above.
Full code:
C:\Sites\rails_projects\sample_app>bundle exec rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0020s
== CreateUsers: migrated (0.0020s) ===========================================
C:\Sites\rails_projects\sample_app>rails console --sandbox
Loading development environment in sandbox (Rails 4.0.1)
Any modifications you make will be rolled back on exit
irb(main):001:0> User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
<ame: "Andrew Ghobrial", email: "email#gmail.com")
=> #<User id: nil, name: "Andrew Ghobrial", email: "email#gmail.com",
created_at: nil, updated_at: nil>
irb(main):004:0> user.save
NameError: undefined local variable or method `user' for main:Object
from (irb):4
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):006:0> user.save
NameError: undefined local variable or method `user' for main:Object
from (irb):6
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):007:0> user
NameError: undefined local variable or method `user' for main:Object
from (irb):7
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'`
So, as #Phlip said, your User.new isn't assigned to a variable. If you do:
user = User.new(name: "Andrew Ghobrial", email: "email#gmail.com")
user.save
That will work, assuming your users table has name and email columns, and your user model looks something like:
class User << ActiveRecord::Base
attr_accessor :name, :email
end
While the capitalisation answers haven't been upvoted, the very first line of the original post IRB proves this was the issue. My resource was created with a lowercase title:
rails g resource user
After the db migration:
Typing "user.count" in IRB gets the NameError
Typing "User.count" gets the expected result (for my empty table):
irb(main):009:0> User.count
(0.4ms) SELECT COUNT(*) FROM "user"
=> 0
So the IRB is case sensitive on active resources and seemingly must be capitalised in declarations. Typing "User" will return a list of the field/column names.
1.9.3-p392 :004 > status.delete_all
in this Status 's' needs to be capitalized to 'S'.
or
in this case
NameError: undefined local variable or method `user' for main:Object
in this Status 'u' needs to be capitalized to 'U'.
or
in this case
NameError: undefined local variable or method `post' for main:Object
in this Status 'p' needs to be capitalized to 'P'.
Make sure that in (Resourse).user, The first letter (pin = Pin, or tweet = Tweet) is capitalized.

Wrong number of arguments when using Squeel

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

adding values to table - ActiveRecord::DangerousAttributeError: update is defined by ActiveRecord

ok guys i have a problem - becouse i created a model and table AdminUser and then i trying to add values to it and then console print me ... any idea?
1.9.3-p286 :020 > AdminUser
=> AdminUser(id: integer, first_name: string, last_name: string, email: string, pass: string, date_reg: datetime, update: datetime)
1.9.3-p286 :021 > AdminUser.all
AdminUser Load (0.2ms) SELECT "admin_user".* FROM "admin_user"
=> []
1.9.3-p286 :022 > me = AdminUser
=> AdminUser(id: integer, first_name: string, last_name: string, email: string, pass: string, date_reg: datetime, update: datetime)
1.9.3-p286 :023 > me.new
ActiveRecord::DangerousAttributeError: update is defined by ActiveRecord
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:91:in `instance_method_already_implemented?'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:263:in `block in define_attribute_method'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:260:in `each'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:260:in `define_attribute_method'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `block in define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `each'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:66:in `block in define_attribute_methods'
from <internal:prelude>:10:in `synchronize'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:63:in `define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:168:in `respond_to?'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:398:in `__run_callback'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_initialize_callbacks'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/base.rb:501:in `initialize'
from (irb):23:in `new'
from (irb):23
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3-p286 :024 >
Your problem is that update is a column in your model. But update is also a method defined by the Active Record. So rails won't know which your talking about if you try and access admin.update. You should be fine if you rename your update column to something else.
You might want to look at timestamps. http://guides.rubyonrails.org/migrations.html#special-helpers it looks to me like what you want to do with the update column.

Resources