error running a scrape command ruby on rails - api

I am trying to re-setup my app on a new computer and run a scrape to build the DB. When I run my first rake scraper:scrape, this is the error I am getting. I am not sure why I am getting this error any help would make my day.. cheers!
Art West#ARTWESTIV ~/desktop/duckduckjeep-master
$ rake scraper:scrape --trace
** Invoke scraper:scrape (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute scraper:scrape
rake aborted!
NoMethodError: undefined method `value' for nil:NilClass
c:/Users/Art West/desktop/duckduckjeep-master/lib/tasks/scraper.rake:17:in `block (3 levels) in <top (required)>'
c:/Users/Art West/desktop/duckduckjeep-master/lib/tasks/scraper.rake:12:in `loop'
c:/Users/Art West/desktop/duckduckjeep-master/lib/tasks/scraper.rake:12:in `block (2 levels) in <top (required)>'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
c:/RailsInstaller/Ruby2.1.0/bin/rake:23:in `load'
c:/RailsInstaller/Ruby2.1.0/bin/rake:23:in `<main>'
Tasks: TOP => scraper:scrape
here is my scraper.rake
namespace :scraper do
desc "Fetch Craigslist posts from 3Taps"
task scrape: :environment do
require 'open-uri'
require 'json'
# Set API token and URL
auth_token = "b077632d17da8857e2fa92c053115e43"
polling_url = "http://polling.3taps.com/poll"
# Grab data until up-to-date
loop do
# Specify request parameters
params = {
auth_token: auth_token,
anchor: Anchor.first.value,
source:"CRAIG",
category_group: "VVVV",
category: "VAUT",
'location.country' => "USA",
retvals: "location,external_url,heading,body,timestamp,price,images,annotations"
}
# Prepare API request
uri = URI.parse(polling_url)
uri.query = URI.encode_www_form(params)
# Submit request
result = JSON.parse(open(uri).read)
# Display results to screen
#puts result["postings"].first["annotations"]["year"]
Anchor.first.update(value: result["anchor"])
puts Anchor.first.value
break if result["postings"].empty?
# #store results in Database
result["postings"].each do |posting|
#ADD HARD FILTER (IN PROGRESS....)
if posting["annotations"]["make"] == "Jeep"
#create new post
#post= Post.new
#post.heading = posting["heading"]
#post.body = posting["body"]
#post.price = posting["price"]
#post.neighborhood = posting["location"]["locality"]
#post.external_url = posting["external_url"]
#post.timestamp = posting["timestamp"]
#post.year = posting ["annotations"]["year"] if posting ["annotations"]["year"].present?
#post.make = posting ["annotations"]["make"] if posting ["annotations"]["make"].present?
#post.model = posting ["annotations"]["model"] if posting ["annotations"]["model"].present?
#post.title_status = posting ["annotations"]["title_status"] if posting ["annotations"]["title_status"].present?
#post.transmission = posting ["annotations"]["transmission"] if posting ["annotations"]["transmission"].present?
#post.mileage = posting ["annotations"]["mileage"] if posting ["annotations"]["mileage"].present?
#post.source_account = posting ["annotations"]["source_account"] if posting ["annotations"]["source_account"].present?
#post.phone = posting ["annotations"]["phone"] if posting ["annotations"]["phone"].present?
#post.lat = posting["location"]["lat"]
#post.lng = posting["location"]["long"]
#post.zipcode = posting["location"]["zipcode"]
#Save Post
#post.save
# Loop over images and save to Image database
posting["images"].each do |image|
#image = Image.new
#image.url = image["full"]
#image.post_id = #post.id
#image.save
end
end
end
end
end
desc "Destroy All Posting Data"
task destroy_all_posts: :environment do
Post.destroy_all
end
desc "Save neighborhood codes in a reference table"
task scrape_neighborhoods: :environment do
require 'open-uri'
require 'json'
# Set API token and URL
auth_token = "b077632d17da8857e2fa92c053115e43"
location_url = "http://reference.3taps.com/locations"
# Specify request parameters
params = {
auth_token: auth_token,
level: "locality",
country: "USA"
}
# Prepare API request
uri = URI.parse(location_url)
uri.query = URI.encode_www_form(params)
# Submit request
result = JSON.parse(open(uri).read)
# Display results to screen
# puts JSON.pretty_generate result
# Store results in database
result["locations"].each do |location|
#location = Location.new
#location.code = location["code"]
#location.name = location["short_name"]
#location.save
end
end
desc "Discard old data"
task discard_old_data: :environment do
Post.all.each do |post|
if post.created_at < 72.hours.ago
post.destoy
end
end
end
end

Your error message is saying it all:
NoMethodError: undefined method `value' for nil:NilClass
You are trying to call value on nil at some point. And, this is probably happening here:
# Specify request parameters
params = {
auth_token: auth_token,
anchor: Anchor.first.value, # this is the line that's creating problem
source:"CRAIG",
category_group: "VVVV",
category: "VAUT",
'location.country' => "USA",
retvals: "location,external_url,heading,body,timestamp,price,images,annotations"
}
When you call this: anchor: Anchor.first.value for the very first time, your Anchor.first is nil and so you are trying to call: nil.value and that's where your rake task fails with the specified error message.
Make sure you have populated the database table (anchors) so that when you call Anchor.first, you don't get nil.
Another approach to avoid this issue would be to use try:
anchor: Anchor.first.try(:value)
That way, your rake task won't fail even if Anchor.first returns nil.

Related

Getting error in ruby web crawling for web page with SSL certificate

require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'openssl'
OpenSSL.debug=true
#url = "http://www.google.com" ## Case 1
url = "Confidential" ## Case 2
#doc = Nokogiri::HTML(open(url)) ## For case 1
doc = Nokogiri::HTML(open(url, :proxy => nil)) ## For case 2
puts doc
Case 1 Output:
I get the output what is expected.
But in Case 2 Output:
OSSL_DEBUG: IS NOW ON!
/usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/net/http.rb:678: warning: error on stack: error:14090086:SSL **routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed**
**/usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/net/http.rb:678:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: error:14090086:SSL** routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed (OpenSSL::SSL::SSLError)
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/net/http.rb:678:in `block in connect'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/timeout.rb:89:in `timeout'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/net/http.rb:678:in `connect'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/net/http.rb:626:in `start'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/open-uri.rb:769:in `buffer_open'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/open-uri.rb:671:in `open'
from /usr/local/rvm/rubies/ruby-1.9.2-p330/lib/ruby/1.9.1/open-uri.rb:33:in `open'
from getData.rb:11:in `<main>'

Getting error trying to start rails server on Vagrant vm

below is the console error message I get when trying to run "rails s" on a vagrant VM. It exits with all this error message, first line being
/home/vagrant/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:878:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
Not sure what is going on here ...
Thank you so much
config\initializers\tire.rb
subdomain = ""
if Rails.env.alpha?
subdomain = ".alpha."
end
if Rails.env.development?
Tire.configure { logger 'log/elasticsearch_development.log', :level => 'debug' }
else
Tire.configure { logger "log/elasticsearch.#{Rails.env.to_s.downcase}.log" }
end
prefix = "org.#{Rails.application.class.parent_name.downcase}#{subdomain}_#{Rails.env.to_s.downcase}"
Tire::Model::Search.index_prefix(prefix)
def get_indices_for_env(prefix)
aliases = Tire::Configuration.client.get(Tire::Configuration.url + '/_aliases').body
global_indices = MultiJson.load(aliases).keys
all_indices = global_indices.select do |index|
index.start_with? prefix
end
all_indices.freeze
end
ALL_INDICES = get_indices_for_env(prefix)
error message
vagrant#precise32:/vagrant$ bundle exec rails s
=> Booting WEBrick
=> Rails 3.2.17 application starting in development on http://x.x.x.x:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server Exiting /home/vagrant/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:878:in
initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:878:in
open'
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:878:in
block in connect'
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/timeout.rb:52:in
timeout'
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:877:in
connect'
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:862:in
do_start'
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:851:in
start'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in
transmit'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in
execute'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.2/lib/tire/http/client.rb:11:in
get'
from /vagrant/config/initializers/tire.rb:20:in get_indices_for_env'
from /vagrant/config/initializers/tire.rb:29:in'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:245:in
load'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:245:in
block in load'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:236:in
load_dependency'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:245:in
load'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/engine.rb:593:in
block (2 levels) in <class:Engine>'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/engine.rb:592:in
each'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/engine.rb:592:in
block in <class:Engine>'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/initializable.rb:30:in
instance_exec'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/initializable.rb:30:in
run'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/initializable.rb:55:in
block in run_initializers'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/initializable.rb:54:in
each'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/initializable.rb:54:in
run_initializers'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/application.rb:136:in
initialize!'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in
method_missing'
from /vagrant/config/environment.rb:5:in <top (required)>'
from /vagrant/config.ru:3:inrequire'
from /vagrant/config.ru:3:in block in <main>'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/builder.rb:51:in
instance_eval'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/builder.rb:51:in
initialize'
from /vagrant/config.ru:innew'
from /vagrant/config.ru:in <main>'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/builder.rb:40:in
eval'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/builder.rb:40:in
parse_file'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/server.rb:200:in
app'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/commands/server.rb:46:in
app'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/server.rb:304:in
wrapped_app'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/server.rb:254:in
start'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/commands/server.rb:70:in
start'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/commands.rb:55:in
block in <top (required)>'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/commands.rb:50:in
tap'
from /home/vagrant/.rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.17/lib/rails/commands.rb:50:in
<top (required)>'
from script/rails:6:inrequire'
from script/rails:6:in `'
Like house9 commented above, I basically commented out the entire ruby file that was causing me the problems.
And then I had a problem starting the Rails server because it couldn't find a database. So it threw an ActiveRecord error:
ActiveRecord::StatementInvalid in PostsController#homepage
Mysql2::Error: Unknown column 'posts.status' in 'where clause': SELECT
posts.* FROM posts WHERE posts.status = 'PUBLISHED' AND
posts.post_type = 'About' AND (title LIKE 'About Ourgoods%') ORDER BY
created_at DESC LIMIT 1....
After connecting a .sql file that I had to the Rails app (following instructions from: How do I connect a mysql database file to a local ruby on rails application), and running rake db:migrate, I finally got rails s working!

Resque with Rails 3 + Heroku + RedisToGo giving Postgresql error

I'm writing an app which needs to send many emails and creates many user notifications because of these emails. This task produces a timeout in Heroku. To solve this, I decided to use Resque and RedistToGo.
What I did was to send the email (it's actually just one email because we use Sendgrid to handle this) and create the notifications using a Resque worker. The email is already created, so I send its id to the worker, along with all the recipients.
This works fine locally. In production, unless we restart our app in Heroku, it only works once. I will post some of my code and the error message:
#lib/tasks/resque.rake
require 'resque/tasks'
task "resque:setup" => :environment do
ENV['QUEUE'] = '*'
end
desc "Alias for resque:work (To run workers on Heroku)"
task "jobs:work" => "resque:work"
#config/initalizers/resque.rb
ENV["REDISTOGO_URL"] ||= "redis://redistogo:some_hash#some_url:some_number/"
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Dir["#{Rails.root}/app/workers/*.rb"].each { |file| require file }
#app/workers/massive_email_sender.rb
class MassiveEmailSender
#queue = :massive_email_queue
def self.perform(email_id, recipients)
email = Email.find(email_id.to_i)
email.recipients = recipients
email.send_email
end
end
I've got an Email model which has an after_create that enqueues the worker:
class Email < ActiveRecord::Base
...
after_create :enqueue_email
def enqueue_email
Resque.enqueue(MassiveEmailSender, self.id, self.recipients)
end
...
end
This Email model also has the send_email method which does what I said before
I'm getting the following error message. I'm gonna post all the information Resque gives to me:
Worker
9dddd06a-2158-464a-b3d9-b2d16380afcf:1 on massive_email_queue at just now
Retry or Remove
Class
MassiveEmailSender
Arguments
21
["some_email_1#gmail.com", "some_email_2#gmail.com"]
Exception
ActiveRecord::StatementInvalid
Error
PG::Error: SSL error: decryption failed or bad record mac : SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"emails"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1139:in `async_exec'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1139:in `exec_no_cache'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:663:in `block in exec_query'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/app/vendor/bundle/ruby/1.9.1/gems/newrelic_rpm-3.3.2/lib/new_relic/agent/instrumentation/active_record.rb:31:in `block in log_with_newrelic_instrumentation'
/app/vendor/bundle/ruby/1.9.1/gems/newrelic_rpm-3.3.2/lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
/app/vendor/bundle/ruby/1.9.1/gems/newrelic_rpm-3.3.2/lib/new_relic/agent/instrumentation/active_record.rb:28:in `log_with_newrelic_instrumentation'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `exec_query'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:1264:in `column_definitions'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:858:in `columns'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `yield'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `default'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `columns'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:237:in `columns_hash'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/relation/delegation.rb:7:in `columns_hash'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:330:in `find_one'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:311:in `find_with_ids'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/relation/finder_methods.rb:107:in `find'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.2/lib/active_record/querying.rb:5:in `find'
/app/app/workers/massive_email_sender.rb:5:in `perform'
According to this, the first argument is the email id, and the second one is the list of all recipients... exactly as it should be.
Can anyone help me? Thanks!
I've run into the same problem. Assuming you're using Active Record you have to call ActiveRecord::Base.establish_connection for each forked Resque worker to make sure it doesn't have a stale database connection. Try putting this in your lib/tasks/resque.rake
task "resque:setup" => :environment do
ENV['QUEUE'] = '*'
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
end

Rails does not read the code from the file enviroment.rb when I use the production environment

I am using rails version 3.0.3.
If I run the server in the development environment, everything works ok, but when trying to run in a production environment then rails skip read the code from the file /config/ enviroment.rb.
I have the following error:
=> Booting WEBrick
=> Rails 3.0.3 application starting in production on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/herman/WORK/bloomnet/broniszeKwiaty/broniszeKwiaty/broniszeKwiaty/app/models/produkt.rb:10: uninitialized constant Produkt::GRUPY_PRODUKTOWE (NameError)
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:138:in `eager_load!'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `each'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `eager_load!'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `each'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `eager_load!'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:108:in `eager_load!'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application/finisher.rb:41
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `run_initializers'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `send'
from /usr/local/lib64/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
from /home/herman/WORK/bloomnet/broniszeKwiaty/broniszeKwiaty/broniszeKwiaty/config/environment.rb:5
from /home/herman/WORK/bloomnet/broniszeKwiaty/broniszeKwiaty/broniszeKwiaty/config.ru:3
from /usr/lib64/ruby/gems/1.8/gems/rack-1.2.2/lib/rack/builder.rb:46:in `instance_eval'
from /usr/lib64/ruby/gems/1.8/gems/rack-1.2.2/lib/rack/builder.rb:46:in `initialize'
from /home/herman/WORK/bloomnet/broniszeKwiaty/broniszeKwiaty/broniszeKwiaty/config.ru:1:in `new'
from /home/herman/WORK/bloomnet/broniszeKwiaty/broniszeKwiaty/broniszeKwiaty/config.ru:1
My enviroment.rb looks like:
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
BroniszeKwiaty::Application.initialize!
#exceptions
CONTROLLERS_NAMES = {"menu" => "menu_index"}
CONTROLLERS_NAMES_IN_VIEW = {"names" => "imieniny", "produkty" => "towary"}
CONTROLLERS_HIDDEN = [7, 15, 18]
$ZABRONIONE_ROZSZERZENIA = %w{ php rb cgi py shtml pl }
# Ustawienia dla zdjec :
$HEIGHT_MINI3 = 106
$WIDTH_MINI3 = 106
$HEIGHT_CAL = 120
$WIDTH_CAL = 120
$HEIGHT_MINI2 = 163
$WIDTH_MINI2 = 163
$HEIGHT_MINI = 174
$WIDTH_MINI = 174
$WIDTH_MATCHED = 500
$HEIGHT_MATCHED = 300
$WIDTH = 800
$HEIGHT = 600
##################################
$default_cal_path = '/images/cal.png'
$default_cal2_path = '/images/cal2.png'
MENU_URL = { 8 => '/kontakt', 2=> "/informacje", 6=>"/najemcy", 5 =>'/hale', 3 => '/oferta-handlowa', 7 => '/multimedia'} #zastepuje adres /podstrony/:id na wartosc z tablicy (url(/podstrony/2) => url(/informacje))
$ID_HK = 3 #id hali kwiatow w bazie bronisz
$ID_WK = 8 #id wiaty kwiatowej w bazie bronisz
PELNA_NAZWA_HALI = {3 => "hala kwiatowa",8 => "wiata kwiatowa"}
SKROT_NAZWY_HALI = {3 => "HK",8 => "WK"}
GRUPY_PRODUKTOWE = ["opakowania do kwiatów","kwiaty cięte, zieleń","rośliny doniczkowe", "dodatki kwiatowe"]
Many thanks for help
.. sometimes, ruby takes a wrong turn when deciding where to look for a constant. You defined the constant GRUPY_PRODUKTOWE globally and you probably access it within the Product model with just "GRUPY_PRODUKTOWE". Try "::GRUPY_PRODUKTOWE" so it knows to look for it in the global scope.

Rails 3: How to render ERb template in rake task?

I am rendering a pretty huge sitemap HTML file with rake. Unfortunately, the code breaks when I migrate to rails 3. My current code looks like this:
#controller = ActionController::Base.new
#controller.request = ActionController::TestRequest.new
#controller.instance_eval do
#url = ActionController::UrlRewriter.new(request, {})
end
# collect data, open output file file
template = ERB.new(IO.read("#{RAILS_ROOT}/app/views/sitemap/index.html.erb"))
f.puts(template.result(binding))
This code worked in 2.3, but breaks in Rails 3 as url_for does not access #controller anymore, but controller. (I think that's why.)
undefined local variable or method `controller' for #<Object:0x3794c>
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:31:in `url_options'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:99:in `url_for'
(erb):5
/Users/me/Documents/Projects/zvg2/lib/tasks/zvg.rake:452
I also tried creating an ActionView to do it like that:
av = ActionView::Base.new(Rails::Application::Configuration.new(Rails.root).view_path, {
# my assigns
}, #controller)
av.class_eval do
include ApplicationHelper
end
f.puts(av.render(:template => "sitemap/index.html"))
But the problem seems to be the same, although ActionView::Base.new takes my controller.
undefined local variable or method `controller' for nil:NilClass
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/whiny_nil.rb:48:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:31:in `url_options'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/Users/me/Documents/Projects/zvg2/app/views/sitemap/index.html.erb:5:in `_app_views_sitemap_index_html_erb__757224102_30745030_0'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/template.rb:135:in `send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/template.rb:135:in `render'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/notifications.rb:54:in `instrument'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/template.rb:127:in `render'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/render/rendering.rb:59:in `_render_template'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/notifications.rb:52:in `instrument'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/notifications.rb:52:in `instrument'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/render/rendering.rb:56:in `_render_template'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/render/rendering.rb:26:in `render'
/Users/me/Documents/Projects/zvg2/lib/tasks/zvg.rake:450
What's the best practice for rendering a template with rake that uses url_for and link_to? I bet as Rails just got more modular, there should be an easy way for just this?
Thanks in advance! Jan
One of the approaches works when I include Rails.application.routes.url_helpers instead of ActionView::Helpers::UrlHelper. This works for now:
include Rails.application.routes.url_helpers # brings ActionDispatch::Routing::UrlFor
include ActionView::Helpers::TagHelper
av = ActionView::Base.new(Rails.root.join('app', 'views'))
av.assign({
:regions => #regions,
:courts_by_region => #courts_by_region,
:cities_by_region => #cities_by_region,
:districts_by_region => #districts_by_region
})
f = File.new(file_name, 'w')
f.puts(av.render(:template => "sitemap/index.html"))
f.close
Hope this helps others. If there is a better solution, I'd be interested.
Also, how do I automatically get a hash of assigns from binding?
I was successfull by doing something like this:
class Template < ActionView::Base
include Rails.application.routes.url_helpers
include ActionView::Helpers::TagHelper
def default_url_options
{host: 'yourhost.org'}
end
end
template = Template.new(Rails.root.join('app', 'views'))
template.assign(attendee: #attendee)
template.render(template: 'sitemap/index.html.erb')
This is what worked for me (Rails 3):
class TaskActionView < ActionView::Base
include Rails.application.routes.url_helpers
include ApplicationHelper
def default_url_options
{host: 'example.com'}
end
end
def action_view
controller = ActionController::Base.new
controller.request = ActionDispatch::TestRequest.new
TaskActionView.new(Rails.root.join('app', 'views'), {}, controller)
end
action_view.render(:template =>"path/to/template")
This successfully included my application helper, tag helpers, and url helpers. You'll want to put your application/environments host properly into the default_url_options hash.
This is what worked for me:
html = render_to_string(:index, layout: nil)