How to access a namespace method in Ruby console? - ruby-on-rails

Found this post Include namespace in Rails 3.1 console but it doesn't seem to work.
The following lib/task defined and it works from the command line: rake namespace_name:task_name.
How to call a method method_name in namespace_name from within the console, without calling the task?
rails console
namespace_name::task_name
NameError: undefined local variable or method 'namespace_name' for main:Object
irb namespace_name
NameError: undefined local variable or method 'namespace_name' for main:Object
Working in Rails 3.07, Ubuntu.

If you want to call a method defined inside a .rake file you do something similar to what #Nate said, but instead of calling the raketask, call the method:
require 'rake'
Rake.load_rakefile 'lib/tasks/namespace_name.rake'
method_name(arg1, arg2)
It feels kind of strange that you don't need to specify the namespaces but I just tried this and it worked.

You're confusing two different kinds of "namespaces" - Ruby modules can perform the task of "namespacing" Ruby code; Rake namespaces are only used within Rake to categorize tasks, and they don't create a module namespace.
The page you linked only works with Ruby module namespaces.
If you want to call Rake tasks from the Rails console, it's a bit more involved...
require 'rake'
Rake.load_rakefile 'lib/tasks/namespace_name.rake'
Rake::Task['namespace_name:task_name'].invoke
Or just call it on the command line from within the Rails console -
%x[rake namespace_name:task_name]

Related

Why is pretty_print not working in this snippet for profiling Rails application memory?

So I'm following along in the Railer Performance book and it tells me to make following code snippet in environment.rb:
MemoryProfiler.report do
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
end.pretty_print(to_file: "test.txt")
Then I'm supposed to run rails runner "puts 'hello world'"
But when I do, I get:
/Users/samantha/.rbenv/versions/2.3.1/lib/ruby/2.3.0/pp.rb:316:in `pretty_print': undefined method `text' for {:to_file=>"test.txt"}:Hash (NoMethodError)
Did you mean? test
Is pretty_print a ruby gem that I am supposed to install?
I think your MemoryProfiler.report do ... end block returns nil (which I don't know why it happens).
When report is present, it executes MemoryProfiler's pretty_print method on an instance of MemoryProfiler::Results.
When report is nil, it executes Ruby's pretty_print method on an instance of a NilClass.

How to resolve Rails, GeoRuby (and postgis/postgresql): NameError: uninitialized constant Point?

when i am running the code on rails console as:
Point.from_x_y(88.365805,22.543538)
Then getting error as:
NameError: uninitialized constant Point
Can anyone help me to remove this error?
I am running require 'gems' before running this code as:
require 'geo_ruby'
require 'geo_ruby/ewk'
It's namespaced, so if you're using it rails instead of pure ruby, you need to call the full namespace of the class. Your query will work if you call instead:
GeoRuby::SimpleFeatures::Point.from_x_y(88.365805,22.543538)

Where to put model “utility” functions in Ruby on Rails, if it is also required in a rake task?

This is a 2nd part to the following question:
Where to put model "utility" functions in Ruby on Rails
Problem is, I need access to these utility functions from a rake task as well. Using the accepted technique in in the other thread, I get an "undefined method" error when accessing my model from a rake task.
What is the best way to fix this?
Thanks
You probably need to define your rake task as dependent on the Rails environment:
task :my_task => :environment do
# Will load Rails stack before executing this block
MyModel.foo
end
The default behavior is to load almost nothing, so you won't have access to your models unless you ask for it.

problem accessing namespaced class in rake task

Given a rake task that references both a namespaced and non-namespaced model:
namespace :thing do
task :thingo => :environment do
Klass.first.some_method
Namespaced::Klass.first.some_other_method
end
end
Using ruby 1.9.2, rails 3.0.9, and rake 0.9.2, this yields an exception, like so:
undefined method 'some_other_method' for #<Klass:0x007fcfafbaa6e0>
Two things:
Why doesn't rails return the proper namespacing in the rake environment (in a debugger session), but it does in a console session?
Why does changing the order of reference work? (That is, if the environment is already calling "Namespaced::Klass" as "Klass", then calling "Klass" should fail with undefined method 'some_method' for #<Klass:0x007fcfafbaa6e0> right?
By the way, I've tried ::Namespaced::Klass.first.some_other_method
If the answer isn't simple, I'll put together a test app - let me know! :-)
First, some background on metaphor shear - two different kinds of namespaces:
Although Rake Namepsaces and Ruby Namespaces share the word Namespace, they are separate concepts. Rake namespaces are just organizing containers for Rake Tasks, not Ruby namespaces/modules. So code inside your thing:thingo rake task is actually executing at the top-level Ruby namespace.
Second: If Klass is a single class not in a namespace, you can reference it directly. If the class exists as Foo::Klass then you'll need to use the fully-qualified Foo::Klass reference unless the scope of the reference is already within the Foo namespace.
Because Rake namespaces aren't Ruby modules, you are not in the context of a Ruby namespace within your task. This is why Klass.some_method works if Klass isn't in a module.
If this doesn't explain the question, please post the class definition for Klass including any module/namespace membership.

Access a class method from a model in the Rakefile / Ruby on Rails 3

I have a model, let's call it Foobar. I want to be able to run a cron job to update an attribute of all objects that are instances of Foobar. So, in pseudocode, it might be something like this:
Foobar.all.each do |foobar|
foobar.update_attributes({:my_attribute => 'updated'});
end
Now, let's say I wrap that in a class method called Foobar.run_update().
Calling Foobar.run_update() would work fine from the controller, or even from a view. But, what I want to do is run run_update() from the Rakefile so that I can tie it into a cron run. But, the Foobar class is not available to Rake when it is called from crontab.
How can I resolve that? How can I access the class methods of Foobars from Rake, when Rake is called from cron?
Thank you very much for your help.
By rake, if you mean a rake task then adding => :environment loads the rails environment for the task and you be able to call the Foobar.run_update method there. Like,
namespace :foobar do
task :update => :environment do
Foobar.run_update
end
end
And you should just be able to call rake foobar:update from the console and have it scheduled as a cronjob.
You can load up the Rails environment by requiring config/environment.rb:
ENV["RAILS_ENV"] ||= "production"
require '/where/your/rails/project/is/config/environment.rb'
In my Sinatra apps I typically have the file models/init.rb which requires Sequel, sets up my DB connection, and then uses require_relative to require all my model files. My main application then does require_relative "models/init".
With this setup any other script (including IRB) all I have to do is require the models/init.rb file myself, and I have full access to the same models and DB connection that the application has.

Resources