Accessing Helpers in Heroku Console - ruby-on-rails

In order to track activity in my application, I built a set of helpers which take in arrays of data and spit out .csv files. I want to use these from the console like so:
helper.export_data(array_of_data)
This works fine on my machine, but the heroku console doesn't seem to let me call helper functions. I receive the error:
NameError: undefined local variable or method `helper' for main:Object

I would guess here that your application is doing some sort of initialisation to get helper loaded into your console, and that that initialisation isn't occurring when you spin up a Heroku console.
Is there anything that you have done in your code which initialises this helper object?

Had same question, and found the answer here:
http://www.funonrails.com/2011/03/accessing-view-helpers-routes-in-rails.html
relevant part is:
>> include ActionView::Helpers
>> => Object
>> include ApplicationHelper
>> => Object
>> include ActionView::Helpers::ApplicationHelper^C
>> display_amount 2500 => "$2,500"

Related

Monkey patching a db model class in Rails with Mongoid causes weird behaviour

I am using a development script file to check out new possible ideas. Recently I tried to monkey patch MyDBObject from within that script file.
Assume an empty dev.rb file and add a monkey patch right in the top like so:
class MyDBObject
def test_function
'function works'
end
end
Starting up the pry console and loading the file yields random results.
First I received:
NoMethodError: undefined method `relations' for MyDBObject:Class
Later the script loaded, but I couldn't access the original class any longer:
undefined method `first' for MyDBObject:Class
I noticed that prepending the line:
MyDBObject
right before the monkey patching, the intended functionality is achieved.
This appears to be some sort of lazy loading of the class objects. Can somebody cast some light on this for me please?
Depending on the order in which source files are loaded, you'll either be redefining the entire class, or having your changes replaced.
I highly recommend giving this a read: http://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/ (TLDR - put your patch in a module and explicitly include it)

Namespace module class methods undefined

I'm trying to use modules for namespacing reasons. I have this file located in my Rails app at /lib/reports/stripe.rb.
module Reports
module Stripe
def self.foo
puts 'i am foo'
end
end
end
In my console, I'd expect to be able to call foo by Reports::Stripe.foo or Reports::Stripe::foo, but I get the error
NoMethodError: undefined method `foo' for Reports::Stripe:Module
What am I doing wrong? Also feel free to let me know if there's a better way to organize the location and namespacing.
All method calls in ruby use the . syntax. Even "module" methods.
> Reports::Stripe.foo
i am foo
You may be receiving the error NoMethodError: undefined method 'foo' for Reports::Stripe:Module if you have added the method after you have started the rails console. Try restarting the console or reloading the file with load 'reports/stripe'.
The file was actually located in /lib/reports/stripe/stripe.rb. It was a mistake I made much earlier, but forgot to fix. Moving the file to /lib/reports/stripe.rb resolved the issue.

Rails/Ruby send method can't build paths with params?

So i found this strange anomaly while working in a gem that we are using internally.
We have this private method
private
def redirect_to_element(element, next_upload)
send("scorecard_#{element.base_class_name.underscore}_path", current_scorecard, current_tab(element, next_upload))
end
Which just builds a path dynamically depending on what element is passed to it. What i would like to do is have those dynamic paths pass some params. But i get this error
undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>
so in the console i tried several things and this is what i found.
>> scorecard_enterprise_development_path
=> "/scorecards/338/enterprise_development"
>> send('scorecard_enterprise_development_path')
=> "/scorecards/338/enterprise_development"
>> scorecard_enterprise_development_path(ignore_tracking: true)
=> "/scorecards/338/enterprise_development?ignore_tracking=true"
>> send('scorecard_enterprise_development_path(ignore_tracking: true)')
!! #<NoMethodError: undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>>
That using the send method to build a path with params will fail. Can anyone explain why this happens?
I am using, ruby -v 1.9.3p327 and rails -v 3.2.16
#send will invoke the method identified by the first argument and pass it any arguments specified.
So you should use the method this way:
send('scorecard_enterprise_development_path', ignore_tracking: true)
See the send documentation

Using find_by_<field> returns "Table doesn't exist" error

I'm working on a Ruby 1.8 / Rails 2.1 application which is connecting to a SQL Server 2008 database.
When I try to use a find_by_CompanyCode method, ActiveRecord returns a NoMethodError. Yet, from the following you can see that the table exists and has the method in question.
Not sure what I'm missing.. any help would be appreciated
EDIT: Only fields which end in "Code" aren't appearing when I just run IvantageEmployee.first .. error occurs from a view. Moving the exact same code to a controller, the code works as expected.
eval #goal.organization_type.employee_class + ".find_by_#{#goal.organization_type.employee_field_code}('#{#goal.specifier}').#{#goal.organization_type.employee_field_description}"
>> IvantageEmployee.first.CompanyCode
=> "GAI"
>> IvantageEmployee.find_by_CompanyCode('GAI')
NoMethodError: undefined method `find_by_CompanyCode' for IvantageEmployee(Table doesn't exist):Class
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1613:in `method_missing_without_paginate'
from /usr/lib/ruby/gems/1.8/gems/will_paginate-2.3.15/lib/will_paginate/finder.rb:170:in `method_missing'
from (irb):7
>>
Some more information. The table has several fields. The one I'm struggling with is CompanyCode, but it also has RegionDescription.
Note the following console output. find_by_RegionDescription works; find_by_CompanyCode doesn't work. Also, CompanyCode doesn't appear when I just output the class, but RegionDescription doesn't Not sure why ActiveRecord would be missing fields that are on the table
>> IvantageEmployee.find_by_RegionDescription 'GAI'
### Finding method find_by_RegionDescription
=> nil
>> IvantageEmployee.find_by_CompanyCode 'GAI'
### Finding method find_by_CompanyCode
NoMethodError: undefined method `find_by_CompanyCode' for IvantageEmployee(Table doesn't exist):Class
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1614:in `method_missing_without_paginate'
from /usr/lib/ruby/gems/1.8/gems/will_paginate-2.3.15/lib/will_paginate/finder.rb:170:in `method_missing'
from (irb):9
>>
You need to use snake case instead of camel case:
IvantageEmployee.find_by_company_code('GAI')
The convention in ruby is to use camel case for classes and snake case for methods. Rails follows that convention(as should you) so you can assume that any methods dynamically created by it are going to be snake case.

Ruby on Rails - using logger.info "hi" gives an undefined variable or method error, what to change/set?

I want to put something in the log, but I keep getting
NameError: undefined local variable or method 'logger' for Report:Class
I've tried
logger "hi"
logger 'hi'
logger.info 'hi'
Rails.logger "hi"
but I just get the above error.
I tried adding config.logger = Logger.new(STDOUT) to my config/environments/development.rb but that didn't help, still gettng the error.
I am running the method in script/console... but I exit and enter it each time to reload everything.
You have to use:
Rails.logger.info 'hi'
Rails.logger returns a logger instance, and by calling info, warn, error or debug on it with a message, this message is logged with the specified log level.
Within an ActiveRecord or ActionController instance you also have a logger convenience accessor available that returns Rails.logger.
Unless you have a variable or method called logger available in the scope you're executing that it won't magically exist. The config context is short-term, during initialization only.
Rails.logger is the global where you can usually access it.
You can use the Rails logger outside of Rails models in at least version 2.3.X.
You might be used to doing the following in your models or controllers:
logger.info "Some debugging info I want to see in my development log."
If you make a regular model that doesn’t inherit from ActiveRecord, though, you may see the following error:
undefined local variable or method `logger' for #
The solution is to call Rails.logger.info (or debug, or warn) as follows:
Rails.logger.info "Some debugging info I want to see in my development log."

Resources