undefined method `scan' for {"tooth"=>""}:Hash - ruby-on-rails

I upgraded to rails4 from 3.2.2 and i now get this undefined method from my rabl template that id didn't get before the upgrade.
the "tooth" paramater on procedures is a set by a getter/setter method that is stored in the postgres database as a hstore hash named properties.
Not sure what has changed in rails4 to make this happen!
Any help would be much appreciated.
ERROR:
Showing /home/action/app/views/procedures/chart.json.rabl where line #1 raised:
undefined method `scan' for {"tooth"=>""}:Hash
Extracted source (around line #1):
1 object #procedures
2 attributes :buccal, :mesial, :occlusal, :distal, :lingual
Trace of template inclusion: app/views/procedures/_procedures_table.html.erb, app/views/procedures/chart.html.erb
Procedures model (getter and setter method for :tooth)
%w[tooth buccal mesial occlusal distal lingual].each do |key|
attr_accessible key
scope "has_#{key}", lambda { |value| where("properties #> (? => ?)", key, value) }
define_method(key) do
properties && properties[key]
end
define_method("#{key}=") do |value|
self.properties = (properties || {}).merge(key => value)
end
end
Gemfile:
source 'http://rubygems.org'
gem 'rails', '4.0.0.rc2'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem "paperclip", "~> 3.0"
gem 'rabl'
gem 'activerecord-postgres-hstore'
gem "bugsnag"
gem 'country_select'
gem 'bullet', group: :development
# Asset template engines
gem 'sass-rails', '~> 4.0.0.rc1'
gem 'coffee-rails', '~> 4.0.0.rc1'
gem 'uglifier', '>= 1.3.0'
gem 'twitter-bootstrap-rails'
gem 'therubyracer', :platform => :ruby
gem 'less-rails'
gem 'jquery-ui-rails'
gem 'jquery-rails'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
# Declarative Authorization
gem 'declarative_authorization'
gem 'protected_attributes'
# Authlogic
gem 'authlogic'
gem 'simple_form', '3.0.0.rc'
group :production do
end
group :test do
# Pretty printed test output
gem 'turn', :require => false
end
gem 'rubber'
gem 'open4'
gem 'gelf'
gem 'graylog2_exceptions', :git => 'git://github.com/wr0ngway/graylog2_exceptions.git'
gem 'graylog2-resque'

Found the answer!! Rails 4 now has native support for Hstore, which is awesome but I hadn't realised. To fix my problem i merely followed this guide below which is great to set up my model with hstore and then deleted my getter/setter methods. http://blog.remarkablelabs.com/2012/12/a-love-affair-with-postgresql-rails-4-countdown-to-2013

Related

rails 5: rails c doesn't show line number on error

I'm running rails 5, upgraded from 4.2.x
In the console and when running methods that fail, the error
doesn't show the line number, I only get to see:
Traceback (most recent call last):
NoMethodError (undefined method `[]' for nil:NilClass)
I made sure development.rb has:
config.log_level = :debug
My gemfile:
source 'https://rubygems.org'
gem 'rails', '5.1.2' # mothership
gem 'bootstrap-sass' # crutches
gem 'sass-rails' # Use SCSS for stylesheets
gem 'devise' # login/sessions
gem 'devise-i18n' # login/sessions
gem 'haml' # markup language
gem 'uglifier', '>= 1.3.0' # Use Uglifier as compressor for JavaScript assets
gem 'coffee-rails'
gem 'jquery-rails' # Use jquery as the JavaScript library
gem 'bcrypt', '~> 3.1.7' # Use ActiveModel has_secure_password
gem 'html5_validators'
gem 'execjs'
gem 'puma'
gem 'pg'
gem 'listen'
group :development do
gem 'rails_db'
gem 'hirb'
gem "better_errors"
gem "binding_of_caller"
end
group :production do
gem 'aws-sdk-rails'
gem 'rails_12factor'
end
What should I change so I get to see line where error occurs?
If you do something like this in the console:
begin
your_method_call
rescue => e
puts e.backtrace
end
You will be able to rescue the exception and print the backtrace. You don't have to puts it, you can do whatever you want with the backtrace, but you get the idea.
Hope this help.

Having errors with rake db:create

I'm trying to install fat free crm with ruby on rails on Windows 7, but when I put the command
rake db:create RAILS_ENV=production
it says
rake aborted!
LoadError: cannot load such -- 2.1/pg_ext
Normally, it would say what gem I should install, but not in this case.
How can I solve this error?
Edit:
A friend of mine suggested to uncomment mysql2 and comment pg on the Gemfile file in the proyect like this:
gem 'mysql2'
#gem 'sqlite3'
#gem 'pg'
But now I get this:
Unable to load the EventMachine C extension; To use the pure-ruby reactor, requi
re 'em/pure_ruby'
C:/Ruby21/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': ca
nnot load such file -- 2.1/rubyeventmachine (LoadError)
Have any ideas of why this happends?
Here is my Gemfile:
source 'https://rubygems.org'
# Uncomment the database that you have configured in config/database.yml
# ----------------------------------------------------------------------
gem 'mysql2'
# gem 'sqlite3'
# gem 'pg'
# Removes a gem dependency
def remove(name)
#dependencies.reject! {|d| d.name == name }
end
# Replaces an existing gem dependency (e.g. from gemspec) with an alternate source.
def gem(name, *args)
remove(name)
super
end
# Bundler no longer treats runtime dependencies as base dependencies.
# The following code restores this behaviour.
# (See https://github.com/carlhuda/bundler/issues/1041)
spec = Bundler.load_gemspec( File.expand_path("../fat_free_crm.gemspec", __FILE__) )
spec.runtime_dependencies.each do |dep|
gem dep.name, *(dep.requirement.as_list)
end
# Remove premailer auto-require
gem 'premailer', :require => false
# Remove fat_free_crm dependency, to stop it from being auto-required too early.
remove 'fat_free_crm'
group :development do
# don't load these gems in travis
unless ENV["CI"]
gem 'thin'
gem 'quiet_assets'
gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails'
gem 'capistrano-rvm'
#~ gem 'capistrano-chruby'
#~ gem 'capistrano-rbenv'
gem 'guard'
gem 'guard-rspec'
gem 'guard-rails'
gem 'rb-inotify', :require => false
gem 'rb-fsevent', :require => false
gem 'rb-fchange', :require => false
end
end
group :development, :test do
gem 'rspec-rails', '~> 2'
gem 'headless'
gem 'debugger', :platforms => 'mri_19' unless ENV["CI"]
gem 'byebug', :platforms => ['mri_20', 'mri_21'] unless ENV["CI"]
gem 'pry-rails' unless ENV["CI"]
end
group :test do
gem 'capybara'
gem 'selenium-webdriver'
gem 'database_cleaner'
gem "acts_as_fu"
gem 'factory_girl_rails'
gem 'zeus' unless ENV["CI"]
gem 'coveralls', :require => false
gem 'timecop'
end
group :heroku do
gem 'unicorn', :platform => :ruby
gem 'rails_12factor'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
gem 'execjs'
gem 'therubyracer', :platform => :ruby unless ENV["CI"]
end
gem 'turbo-sprockets-rails3'
It sounds like you are missing the pg gem from your Gemfile. Is it in the development group?
Can you post your Gemfile?
Uncomment #gem pg.
Unable to load the EventMachine C extension; To use the pure-ruby reactor, require 'em/pure_ruby' is a separate error. Where did you get that Gemfile?

digest_path & asset_digest_path not allowing digest URLs

I have been having this pretty rough time with assets on production. It came down to me trying to override the sprokets helper module to try and see what was up... When I re-wrote it to the following:
module Sprockets
module Rails
module Helper
def compute_asset_path(path, options = {})
It would not run. I tried it in my local environment, and it runs perfectly. Is there a reason the production environment would not allow my assets to render in digest form, but my local environment would? This is related to what this gent was asking...
Rails 4.0.3 generating incorrect asset paths with asset_sync
Here is the error I receive after trying to override that method (which doesn't have an error when running with RAILS_ENV=development):
ActionView::Template::Error (undefined local variable or method `digest_path' for #<#<Class:0x000001034a99e0>:0x000001034a81d0>):
Asset Gems in Gemfile for reference:
source 'http://rubygems.org'
# ruby '2.1.1'
gem 'rails', '4.0.4'
gem 'jbuilder', '~> 1.2'
gem 'devise'
gem 'devise_invitable'
gem 'figaro'
gem 'mysql2'
gem 'simple_form'
gem 'kaminari'
gem 'statistics'
gem 'possessive'
gem 'geocoder'
gem 'nokogiri'
gem 'asset_sync'
gem 'sprockets-rails', :require => 'sprockets/railtie'
gem 'ledermann-rails-settings', :require => 'rails-settings'
gem 'public_activity'
group :assets do
gem 'therubyracer'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
end
group :development do
gem 'better_errors'
gem 'binding_of_caller', :platforms=>[:mri_19, :mri_20, :rbx]
gem 'guard-bundler'
gem 'guard-rails'
gem 'quiet_assets'
gem 'rails_layout'
gem 'rb-fchange', :require=>false
gem 'rb-fsevent', :require=>false
gem 'rb-inotify', :require=>false
end
group :test do
gem 'email_spec', '>= 1.4.0'
gem 'launchy', '>= 2.2.0'
gem 'capybara', '>= 2.0.3'
gem 'database_cleaner', '>= 1.0.0.RC1'
gem 'cucumber-rails', '>= 1.3.1', :require => false
end
group :production do
gem 'rails_12factor'
end
gem 'rspec-rails', '>= 2.12.2', :group => [:development, :test]
gem 'factory_girl_rails', '>= 4.2.0', :group => [:development, :test]
gem 'teaspoon', '>= 0.7.4', :group => [:development, :test]
gem 'cancan', '>= 1.6.9'
gem 'rolify', '>= 3.2.0'
gem 'stripe-rails'
gem 'faker'
gem 'open4'
gem 'unf'
When I run this in development mode, it runs completely fine. When I run this through production mode (even with the same config file) it does not inherit the View properties like digest_path or asset_digest_path is null or manifest, etc.
module Sprockets
module Rails
module Helper
def compute_asset_path(path, options = {})
if digest_path = asset_digest_path(path)
path = digest_path if true # BUG: digest assets doesn't work on live, let's just bake it
path += "?body=1" if options[:debug]
File.join(assets_prefix || "/", path)
else
super
end
end
def asset_digest_path(path, options = {})
if manifest = assets_manifest
if digest_path = manifest.assets[path]
return digest_path
end
end
if environment = assets_environment
if asset = environment[path]
return asset.digest_path
end
end
end
end
end
end
module ActionView
module Helpers
module AssetUrlHelper
def compute_asset_path(source, options = {})
dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || ""
File.join(dir, source)
end
end
end
end
Hopefully this will help save my fellow programmer friends some head banging :D
I was uploading the files to S3, I didn't realize that the manifest wasn't loaded by Rails. You can have all your production settings right (like above and in other threads), but if you don't have the manifest.json file readable by Rails, it will still generate /javascript/* (example) urls.
I was still having trouble with multi_json gem's latest version, so I downgraded it to 1.7.8 and it works fine.
gem 'multi_json', '1.7.8'
That's so it can read the manifest.json file which rake assets:precompile creates.
There is a debate on this sprockets thread https://github.com/rails/sprockets-rails/issues/107 on whether your manifest file should be in git or just on a deploy script, do what suits you best, just make sure it is findable in:
/public/assets/manifest.json
or specifiy it yourself with
config.assets.manifest = '...'
That may or may not be depricated.
Cheers!

ActiveAttr stops working on production server

I have created a simple model for handling a contact form:
class ContactForm
include ActiveAttr::Model
attribute :name
attribute :email
attribute :message
attr_accessible :name, :email, :message
validates_presence_of :name, :email, :message
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9] {2,4}$/i
end
It works fine on the development environment, but gives me an error on the production server, logged in the unicorn log:
E, [2013-01-26T18:15:42.724973 #8940] ERROR -- : uninitialized constant ContactForm::ActiveAttr (NameError)
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.11'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem 'bootstrap-sass'
gem 'font-awesome-sass-rails'
# gem 'acts_as_tree', :git => 'git://github.com/amerine/acts_as_tree.git'
gem 'closure_tree'
gem 'kaminari'
gem 'friendly_id'
gem 'slim'
gem 'gmaps4rails'
gem 'devise'
gem 'omniauth'
gem 'oauth2'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem 'simple_form'
gem 'pg_search'
gem 'stamp'
gem 'acts-as-taggable-on', '~> 2.3.1'
gem 'rmagick'
gem 'carrierwave'
gem 'active_attr'
gem 'select2-rails'
# gem 'roo'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
gem 'compass-rails'
end
group :development do
gem "better_errors"
gem "binding_of_caller"
gem 'guard'
gem 'letter_opener'
end
group :test do
gem 'minitest'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
gem 'unicorn'
# Deploy with Capistrano
gem 'capistrano'
# To use debugger
# gem 'debugger'
gem 'bullet', :group => :development
gem 'rack-mini-profiler'
I had the same problem and the fix for me was to specify the version of active_attr in my gemfile.
gem 'active_attr', '0.7.0'
I found an issue on GitHub where someone else had the same problem here.
I have the same problem in development environment.
I followed this step-by-step tutorial https://github.com/thomasklemm/email_form_rails.
When I try to create a new message in console I get :
m=Message.new
NameError: uninitialized constant Message::ActiveAttr
I use Rails 3.2.1, active_attributes gem version is 0.0.1.
EDIT
My fault it seems I made a mistake when installing the gem (not the good one)
change gemfile from
gem 'active_attributes','0.0.1'
to
gem 'active_attr', '0.0.7'
Now it seems to work...

jruby on rails with warbler and tomcat and windows

I tried to deploy an application.war file into webapps/ in the tomcat folder and started up the server. When i try to open http://localhost:8080/relConnections it shows on the screen that "something went wrong". I deleted the common index.html in the public folder in usual rails apps and tchanged the root to my home page.
When i see the log file it says the following:
"An exception happened during JRuby-Rack startup private method `gsub' called for nil:NilClass
--- System
jruby 1.6.7"
and three or four more excecption saying the same lines gsub nil:NilClass for all of those.
Here is my Gemfile
source 'http://rubygems.org'
gem 'jruby-jars', '1.6.7'
gem 'warbler', '1.3.2'
gem 'rails', '3.1.3'
gem 'rake', '0.9.2.2'
gem 'jruby-openssl', '0.7.5', :platforms => :jruby
gem 'json', '1.6.5', :platforms => [:jruby, :mingw, :mingw_19]
gem 'therubyrhino', :platforms => :jruby
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'activerecord-jdbc-adapter', '1.2.2'
gem 'activerecord-oracle_enhanced-adapter', '1.4.0'
gem 'mechanize', '1.0.0'
gem 'nokogiri', '1.5.0'
group :assets do
gem 'sass-rails', '~> 3.1.4'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
group :development do
gem 'ruby-debug-base', '0.10.4'
gem 'ruby-debug-ide', '0.4.17.beta8'
gem 'pry', '0.9.7.4'
end
group :test do
gem 'turn', '0.8.3', :require => false
gem 'minitest', '2.11.0'
gem 'timecop', '0.3.5'
end
gem 'jruby-pageant', '~>1.0.2'
gem 'net-ssh', '~>2.5.2'
And here is my warble.rb
Warbler::Config.new do |config|
#commented code
config.dirs = %w(app config lib log vendor tmp)
#more commented code
end
Let me know if iam missing any gems or anything else here.
Thanks
I think you may try to set the jruby version to 1.9. In the warble.rb
config.webxml.jruby.compat.version = "1.9"

Resources