How to disable build-in Test - ruby-on-rails

I created my model whitch is Test, and everything is working ok when I am using it within browser, but when I try to do something with it from rails c, I got error.
1.9.3p125 :001 > t=Test.find(4)
NoMethodError: undefined method `find' for Test:Module
from (irb):1
from /home/dorijan/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /home/dorijan/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /home/dorijan/.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>'
So is there a way to disable this temporary? I know I shouldnt use this, but now it is too late...:(
Thank you

I dont know how to disable the Test module in rails console, sorry.
One workaround is to namespace your model into a new module and then at the console call from within its new name space see how to here

Problem may be that you have used 'test' which is on the rails reserved word wiki as possibly problematic see
rails reserved words
Try another model name not on reserved list

Related

Is it safe to use 'Function' as a model name in Ruby and Rails?

Is it safe to use 'Function' as a model name in Ruby and Rails?
class Function < ActiveRecord::Base
...
end
I expect some conflicts with some defaults classes, but maybe I'm wrong.
If its not in the reserved method and class names its generally ok to use
see list here
http://www.rubymagic.org/posts/ruby-and-rails-reserved-words
but using function might be confusing when reading the code or talking about it..
"See the function in Function class and make sure it functions properly"
You can never be sure about potential name conflict. It may be fine at some point, but later, some library that you depend on may decide to use that name. A practical strategy people take is to put everything in a namespace (module) with a name you believe to be safe at that point. If that later conflicts with something, you can simply rename that namespace everywhere in your code without touching anything else.
c700595-3:VtM deh0002a$ rails c
Loading development environment (Rails 4.2.0)
2.1.5 :001 > Function
NameError: uninitialized constant Function
from (irb):1
from /Users/deh0002a/.rvm/gems/ruby-2.1.5#vtm/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /Users/deh0002a/.rvm/gems/ruby-2.1.5#vtm/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /Users/deh0002a/.rvm/gems/ruby-2.1.5#vtm/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/deh0002a/.rvm/gems/ruby-2.1.5#vtm/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/deh0002a/.rvm/gems/ruby-2.1.5#vtm/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.1.5 :002 >
So you will be safe to declare a class like that.

Rails Console Error

When I open Rails console, or sandbox for that matter, and I try and add new date, in this case, a user, I keep getting this same error. I have tried everything to get it resolved, but for the life of me I cannot figure it out. I am using Windows, so obviously I know that is one of my first problems, but its all I have for the current moment, so I will have to make due. Here is the error message that pops up when I try and simply enter user.new(information...)... any help would be greatly appreciated, I am pulling my hair out over here.
C:\TTS\Rails\workspace\sample_app>rails console
DL is deprecated, please use Fiddle
Loading development environment (Rails 4.2.0)
irb(main):001:0> user.new(name: "Will Wagar", email: "will#wagar.com")
NameError: undefined local variable or method `user' for main:Object
from (irb):1
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
You almost certainly want User.new (note the capital U) rather than user.new, it's a Ruby convention that classes are capitalised.
To expand, you probably want to do it in several steps:
user = User.new(name: "Will Wagar", email: "will#wagar.com")
user.valid?
user.save
etc...
So in the first one you're creating an instance of the class User and assigning it to the variable user and then in subsequent operations, you're working with the new user variable you've created.
I see that now, I should have mentioned in the question that I have tried it both ways, capitalized and not. I can get a return when I try it with a capital U, but when I try and check to see if it is valid or save it, I get the same error.
irb(main):008:0> user.valid?
NameError: undefined local variable or method user' for main:Object
from (irb):8
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:instart'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:inconsole'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in run_command!'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in'
from bin/rails:4:in require'
from bin/rails:4:in'

Rails: Test module defined by default in all modules but actually... not really

If I do the following in irb:
module Useless ; end
Useless.const_defined? 'Test'
It obviously returns
=> false
But when I do the same in any of my rails apps console, the answer is:
=> true
My first guess was that I was somehow conflicting with a test framework. I was wondering which one and it became strange when I noticed that even if "true" was returned, it seemed to be nevertheless undefined ...
irb(main):004:0> Useless::Test
NameError: uninitialized constant Useless::Test
from (irb):4
from /home/laurent/.rvm/gems/ruby-2.0.0-p0#nanoscripts/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /home/laurent/.rvm/gems/ruby-2.0.0-p0#nanoscripts/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /home/laurent/.rvm/gems/ruby-2.0.0-p0#nanoscripts/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Does someone have an explanation for this ?
Of course I know I should avoid to name a module "Test", but the point is that I am generating dynamically some classes and modules into a module that I use as kind of name space from database introspection. Therefore nothing prevents a table to be named test (this is how I discovered the thing) which would trigger the creation of a class named:
MyNameSpaceForDynamicallyCreatedObjectsFromDatabase::Test
Basically what should I do ?
Hardcode something for test ? and then is there an exhaustive list of table names I should hardcode something for ?
Is there another way to determine if a constant is present in a module without interfering with whatever underlying mechanism I am conflicting with ?
Any help appreciated.
Rgds
Actually I was able to work around by doing a constants.include? 'Test'.to_sym instead of const_defined?:
module Useless ; end
Useless.constants.include? 'Test'.to_sym
Correctly returns the expected:
=> false
And this both in raw ruby and in rails.
The only drawback being that constants returns an array of symbols.
This solves my problem but doesn't explain the why of this const_defined? behaviour.

No deprecated_block_helpers in Rails 3.0.5

When I'm trying to launch my production server on Rails 3.0.5, I keep getting this error:
/var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/core_ext/module/introspection.rb:70:in `const_get': no such file to load -- action_view/helpers/deprecated_block_helpers (LoadError)
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/core_ext/module/introspection.rb:70:in `local_constants'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/core_ext/module/introspection.rb:70:in `each'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/core_ext/module/introspection.rb:70:in `local_constants'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/core_ext/module/introspection.rb:68:in `each'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/core_ext/module/introspection.rb:68:in `local_constants'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/core_ext/module/introspection.rb:86:in `local_constant_names'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:100:in `new_constants'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:91:in `each'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:91:in `new_constants'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:599:in `new_constants_in'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:225:in `load_dependency'
from /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require'
from /home/punkweek/punkweek/config.ru:3
from /var/lib/gems/1.8/gems/rack-1.2.2/lib/rack/builder.rb:46:in `instance_eval'
from /var/lib/gems/1.8/gems/rack-1.2.2/lib/rack/builder.rb:46:in `initialize'
from /home/punkweek/punkweek/config.ru:1:in `new'
from /home/punkweek/punkweek/config.ru:1
It seems there's a problem with a file, deprecated_block_helpers, which is not found in Rails. Anyone knows how to solve this problem?
Actually this is a bug in Rails 3.0.5 (it has been fixed in edge).
DeprecatedBlockHelpers is autoloaded in ActionView::Helpers, but the file itself was removed in 9de8305
This helper was designed to show a deprecation notice for using <% %> instead of <%= %> (or - instead of = in haml) for certain blocks. I'm not sure exactly which blocks the deprecation is for, but maybe take a look through your templates and see if you can trial-and-error this into working.
Alternatively, you can vendor Rails and remove the DeprecatedBlockHelpers autoload, or track edge Rails.

Undefined method '>>' for class 'Date' in Rails 3 on Ruby 1.8.7/1.9.2

I'm running a Rails 3.0.0 application on Ruby 1.8.7-p174. Everything was going swimmingly until I tried to run some tests:
/Users/avand/.rvm/gems/ruby-1.8.7-p174/gems/activesupport-3.0.0/lib/active_support/core_ext/date/calculations.rb:9: undefined method `>>' for class `Date' (NameError)
from /Users/avand/.rvm/gems/ruby-1.8.7-p174/gems/activesupport-3.0.0/lib/active_support/ruby/shim.rb:12:in `require'
from /Users/avand/.rvm/gems/ruby-1.8.7-p174/gems/activesupport-3.0.0/lib/active_support/ruby/shim.rb:12
from /Users/avand/.rvm/gems/ruby-1.8.7-p174/gems/actionpack-3.0.0/lib/abstract_controller.rb:6:in `require'
from /Users/avand/.rvm/gems/ruby-1.8.7-p174/gems/actionpack-3.0.0/lib/abstract_controller.rb:6
I took a look into that Calculations class, noting that undef was being called with :>>. But Ruby 1.8.7 Dates don't have a >> method. I figured I'd wrap it with a condition: if respond_to?(:>>). Things broke further along this time:
/Users/avand/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-3.0.0/lib/active_support/core_ext/date/calculations.rb:91:in `alias_method': undefined method `+' for class `Date' (NameError)
from /Users/avand/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-3.0.0/lib/active_support/core_ext/date/calculations.rb:91:in `<class:Date>'
from /Users/avand/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-3.0.0/lib/active_support/core_ext/date/calculations.rb:7:in `<top (required)>'
from /Users/avand/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-3.0.0/lib/active_support/ruby/shim.rb:12:in `require'
from /Users/avand/.rvm/gems/ruby-1.9.2-rc2/gems/activesupport-3.0.0/lib/active_support/ruby/shim.rb:12:in `<top (required)>'
from /Users/avand/.rvm/gems/ruby-1.9.2-rc2/gems/actionpack-3.0.0/lib/abstract_controller.rb:6:in `require'
The second stack trace is Ruby 1.9.2. I'm getting the same error with Ruby 1.9.2 without my respond_to? check as Ruby 1.8.7 with it.
I commented out my Date extensions in lib. This only occurs in the test environment.
Thoughts?
So this may not be the most helpful answer but it's all I've been able to determine so far.
The file in question: activesupport-3.0.0/lib/active_support/core_ext/date/calculations.rb removes the definitions of :>> as you saw. If the file is require twice, the second loading of that file will fail due to the method no longer being defined on the Date class.
So why is this file being required twice? That I'm really not sure of. I've seen that the protection against this can be buggy (if you'd call it a bug, it may just be a limitation) when you specify the file with a full path once and then another time you depend on the LOAD_PATH having the correct folder in it to find you file that way.
I'd look through your code and see what is requiring either rails/all or activesupprt/railstie and hopefully you'll see two separate places that look slightly different. Also see if you're mucking with the LOAD_PATH anywhere between the two places.

Resources