Access my model using a daemon on rails 3 - ruby-on-rails

I have the following script:
#!/usr/bin/ruby
require 'rubygems' unless defined?(Gem)
require 'mongoid'
include Mongoid::Document
#classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a
puts #classes.count
But I always get:
classes_notification.rb:6:in `': uninitialized constant Availability (NameError)
Some help please, I need to make a daemon to send emails with the information on my availability model.
Thanks in advance.
UPDATE CODE
my script is under app/script/user/remeber_classes.rb
how can I access the require File.dirname(FILE) + "/../../config/environment"
because I'm getting
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in
require': cannot load such file -- ./../../config/environments
(LoadError) from
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in
require' from remember_class.rb:5:in `'

you must include rails environment first
add require "path_to_the_application/config/environment" to your code
or
you can always access the database directly using mongo gem
see this
https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial

Related

Sidekiq Install - uninitialized constant ActiveRecord::Base (NameError)

I am trying to integrate Sidekiq with my Rails Mongoid app. I have been following the steps as outlined Here
I also have redis & mongodb running locally. However after I run bundle then when I try to start my server I get this error....
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sidekiq-2.17.7/lib/sidekiq/rails.rb:4:in `hook_rails!': uninitialized constant ActiveRecord::Base (NameError)
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sidekiq-2.17.7/lib/sidekiq/rails.rb:16:in `block in <class:Rails>'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
from C:/Users/example/Documents/Workspace/app/config/environment.rb:5:in `<top (required)>'
My environment.rb file is as follows...
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
App::Application.initialize!
I previously had resque with redis working with mongoid in this app, so its a sidekiq issue. I don't have a database.yml file since I am using mongoid. I also added the steps for kiqstand but I am getting the same error. Unsure how to resolve?
Edit: I'm 90% sure its because Sidekiq is looking for ActiveRecord, however I removed ActiveRecord cause I'm using Mongo instead. My application.rb has...
# remove activerecord and run off of mongo only
#require 'rails/all'
#http://stackoverflow.com/a/9327651/1026266
%w(
action_controller
action_mailer
active_resource
rails/test_unit
sprockets
).each do |framework|
begin
require "#{framework}/railtie"
rescue LoadError
end
end
Now I just need to figure out how to tell sidekiq to not look for it?
Lack of ActiveRecord was the issue, looks like there was a pull request - https://github.com/mperham/sidekiq/pull/1090
But that was rejected, so I just had to add ActiveRecord to my app and add a dummy database.yml file.

Creating generators Rails

I am having a problem creating a generator in rails this is the error I am getting.
I am trying to create a generator that creates does SMS verification through the provider Twilio.
require "sms_auth-twilio"
NameError: uninitialized constant Twilio::Rails
from /Users/sherodtaylor/.rvm/gems/ruby-1.9.3-p125/gems/sms_auth-twilio-0.0.2/lib/sms_auth-twilio.rb:3:in `<module:Twilio>'
from /Users/sherodtaylor/.rvm/gems/ruby-1.9.3-p125/gems/sms_auth-twilio-0.0.2/lib/sms_auth-twilio.rb:2:in `<top (required)>'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from /Users/sherodtaylor/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'
This is my code
require "sms_auth-twilio/version"
module Twilio
class TwilioGenerator < Rails::Generator::Base
source_root File.expand_path("../generator", __FILE__)
def add_twilio_initializer
copy_file "twilio.rb", "config/initializer/twilio.rb"
end
# config info for the API
def twilio_config_info
copy_file "twilio.yml", "config/twilio.yml"
end
# Verification Controller to verify the company
def copy_verifications
copy_file "verifications_controller", "app/contoller/verifications_controller.rb"
end
end
end
Restarting your Rails server and then try
For your Help see this link : Rails Adding Twilio Sub Accounts : Uninitialized constant User::Twilio
I can't say for sure, but this is not typically how I see generators created within a gem. Usually they are separate from the actual gem_name.rb file. I think if you move the generator logic into the more common lib/generators/generator_name/generator_name.rb you'll have more luck. And then you can place the files to be copied within a subdir ../generator_name/templates/
Also note you need to add the .rb ending to your copy_file "verifications_controller,"
For a template to work off, you can follow the layout of a simple gem I wrote here which utilizes a generator.

Why do do I get a "LoadError"?

I have two Ruby scripts, and I am calling those files and I am getting an error.
I have r1.rb and r2.rb. When I call r1.rb and r2.rb from r3.rb I get this error:
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- r1 (LoadError)
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from r3.rb:1:in `<main>'
r1.rb is:
def random
rand(1000000)
end
r2.rb is:
def random
(rand(26) + 65).chr
end
r3.rb is:
require 'r1'
require 'r2'
puts random
thumb rules when you use require, always use path in require statement [mostly when you use windows platform]
if you use load you need to place filename.rb but no need to pass file path. [First Check the requirement and use load because it is load every time in memory when call]
here I am modifying your code
require 'C:/PLACE YOUR FILE PATH HERE/r1'
require 'C:/PLACE YOUR FILE PATH HERE/r2'
puts random
Please let me know if this works for you
let me add one more things
Same way when you use irb, you have to do same way
>irb
irb(main):001:0> require 'c:/rubycode/test.rb'
=> true
#jdoe is suggest require_relative that's good approach
but I am getting below error
irb(main):001:0> require_relative 'test'
LoadError: cannot infer basepath
from (irb):1:in `require_relative'
from (irb):1
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):002:0> require_relative 'rubycode/test'
LoadError: cannot infer basepath
from (irb):2:in `require_relative'
from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):003:0> require_relative 'c:/rubycode/test'
LoadError: cannot infer basepath
from (irb):3:in `require_relative'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):004:0> require 'c:/rubycode/test'
=> true
irb(main):005:0>

How to require ActiveResource

I want to test my RESTful resource in Rails:
require 'rubygems'
require 'activeresource'
class Event < ActiveResource::Base
self.site = "http://localhost:3000"
end
events = Event.find(:all)
puts events.map(&:name)
I have installed gem:
gem install activeresource
But when I launch my code I get the error message:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- activeresource (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from service.rb:2:in `<main>'
How can I require ActiveResource?
There is a typo in your require, the following is the correct one:
require 'active_resource'
With bunder you can do this just with
gem "activeresource", require: "active_resource"
Normally gems requires file with name equal to gem’s name, with gem "foo" :require => bar syntax you can specify different file to be included by default.
i think you can do it this way:
require 'rubygems'
gem 'activeresource'
require 'active_resource'
You can also specify which version of activeresource you want by
gem 'activeresource', '=3.0.7'
for example. Or you can omit this line completely, but this way it's cleaner... and it makes sure that you get the right version.
Hope this helps, NoICE

Rails 3: Error when running a simple test using RailsGuides instructions

I try to run a simple test following RailsGuides instructions. I have a default structure of the test folder. Here is the relevant part:
test
unit
job_test.rb
test_helper.rb
When I run:
ruby unit/job_test.rb
from the test directory, I get:
<internal:lib/rubygems/custom_require>:29:in `require':
no such file to load -- test_helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from unit/job_test.rb:1:in `<main>'
Here is my job_test.rb:
require 'test_helper'
class JobTest < ActiveSupport::TestCase
test "My First Test" do
assert false
end
end
Is this a known Rails 3 issue ?
Any workarounds ?
Try to include test helper like this
require File.dirname(__FILE__) + '/../test_helper'

Resources