uninitialized constant Active Scaffold rails 2.3.5 - ruby-on-rails

I update my rails application 2.0.2 to 2.3.5. I use active scaffold for the administration part.
I change nothing in my code but a problem is coming with the update.
I have a controller 'admin/user_controller' to manage users.
Here is the code of the controller:
class Admin::UserController < ApplicationController
layout 'admin'
active_scaffold :user do |config|
config.columns.exclude :content, :historique_content, :user_has_objet, :user_has_arme, :user_has_entrainement, :user_has_mission, :mp, :pvp, :user_salt, :tchat, :notoriete_by_pvp, :invitation
config.list.columns = [:user_login, :user_niveau, :user_mail, :user_bloc, :user_valide, :group_id] #:user_description, :race, :group, :user_lastvisited, :user_nextaction, :user_combats_gagner, :user_combats_perdu, :user_combats_nul, :user_password, :user_salt, :user_combats, :user_experience, :user_mana, :user_vie
config.create.link.page = true
config.update.link.page = true
config.create.columns.add :password, :password_confirmation
config.update.columns.add :password, :password_confirmation
config.create.columns.exclude :user_password, :user_salt
config.update.columns.exclude :user_password, :user_salt
config.list.sorting = {:user_login => 'ASC'}
config.subform.columns = []
end
end
This code hasn't change with the update, but when I go in this page, I got this error:
uninitialized constant Users
/Users/Kiva/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:443:in `load_missing_constant'
/Users/Kiva/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in `const_missing'
/Users/Kiva/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:92:in `const_missing'
/Users/Kiva/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/inflector.rb:361:in `constantize'
/Users/Kiva/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/inflector.rb:360:in `each'
/Users/Kiva/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/inflector.rb:360:in `constantize'
/Users/Kiva/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/string/inflections.rb:162:in `constantize'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/extensions/reverse_associations.rb:28:in `reverse_matches_for'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/extensions/reverse_associations.rb:24:in `each'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/extensions/reverse_associations.rb:24:in `reverse_matches_for'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/extensions/reverse_associations.rb:11:in `reverse'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/column.rb:117:in `autolink?'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/active_scaffold.rb:107:in `links_for_associations'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/columns.rb:62:in `each'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/active_scaffold/data_structures/columns.rb:62:in `each'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/active_scaffold.rb:106:in `links_for_associations'
/Users/Kiva/Documents/Projet-rpg/jeu/vendor/plugins/active_scaffold/lib/active_scaffold.rb:59:in `active_scaffold'
/Users/Kiva/Documents/Projet-rpg/jeu/app/controllers/admin/user_controller.rb:11
I search since 2 days but I don't find the problem, can you help me please.

the naming convention in your application seems buggy
it should be:
class Admin::UsersController < ApplicationController
not UserController as it is. maybe it won't fix your problem, but it worth a try!

It seems to be a problem with the way the Model is defined. I had the very same issue when accessing tables in the database with a rake backup task. The problem was that I had a table defined in the database through "belongs_to". I didn't think I needed a model for that table but constantize kept failing. Introducing the model fixed the problem.
Table : users_clients
Model : UsersClient

Starting with Rails 2.3.x you have to install the Render Component plugin:
./script/plugin install git://github.com/ewildgoose/render_component.git -r rails-2.3
See instructions here: https://github.com/activescaffold/active_scaffold/wiki/getting-started

Related

Sudden "uninitialized constant PurIssue" error on STI_preload while upgrading rails to 7.0.1

I am in the process of upgrading a ~2 year old Rails app from 6.1 to 7.0.1
I have an STI setup where Pursuit is the main class and it has several other types as descendants:
# app/models/pursuit.rb
require 'sti_preload'
class Pursuit < ApplicationRecord
# ...
end
Descendant classes all look like this:
# app/models/pur_issue.rb
class PurIssue < Pursuit
# ...
end
--
# app/models/pur_goal.rb
class PurGoal < Pursuit
# ...
end
--
# app/models/pur_tracker.rb
class PurTracker < Pursuit
# ...
end
I have been using STI preload snippet as recommended by the Ruby Guides, and all worked well under Rails 6.0 and 6.1.
But now that I am upgrading to Rails 7.0.1, I suddenly get an "Uninitialized Constant" error for only the PurIssue subclass whereas all the other subclasses load fine:
Showing /Users/me/gits/rffvp/app/views/pursuits/_stats.html.slim where line #1 raised:
uninitialized constant PurIssue
Extracted source (around line #33):
33 types_in_db.each do |type|
34 logger.debug("Preloading STI type #{type}")
35 type.constantize
36 end
37 logger.debug("Types in database #{types_in_db}")
Trace of template inclusion: #<ActionView::Template app/views/layouts/_footer.html.slim locals=[]>, #<ActionView::Template app/views/layouts/application.html.slim locals=[]>
Rails.root: /Users/me/gits/rffvp
Application Trace | Framework Trace | Full Trace
lib/sti_preload.rb:33:in `block in preload_sti'
lib/sti_preload.rb:31:in `each'
lib/sti_preload.rb:31:in `preload_sti'
lib/sti_preload.rb:13:in `descendants'
app/models/pursuit.rb:71:in `<class:Pursuit>'
app/models/pursuit.rb:54:in `<main>'
app/models/pur_issue.rb:52:in `<main>'
app/views/pursuits/_stats.html.slim:1
app/views/layouts/_footer.html.slim:14
app/views/layouts/application.html.slim:13
I can't seem to figure out why PurIssue will not load anymore whereas all the other subclasses will. This is breaking my entire app since PurIssues are the most important data points.
Can someone point me to a Rails configuration change between 6.0, 6.1 and 7.0 that might cause this different behavior?
# lib/sti_preload.rb
module StiPreload
unless Rails.application.config.eager_load
extend ActiveSupport::Concern
included do
cattr_accessor :preloaded, instance_accessor: false
end
class_methods do
def descendants
preload_sti unless preloaded
super
end
# Constantizes all types present in the database. There might be more on
# disk, but that does not matter in practice as far as the STI API is
# concerned.
#
# Assumes store_full_sti_class is true, the default.
def preload_sti
types_in_db = \
base_class
.unscoped
.select(inheritance_column)
.distinct
.pluck(inheritance_column)
.compact
types_in_db.each do |type|
logger.debug("Preloading STI type #{type}")
type.constantize
end
logger.debug("Types in database #{types_in_db}")
self.preloaded = true
end
end
end
end
Zeitwerk shows the same error by the way:
$ rails zeitwerk:check
Hold on, I am eager loading the application.
rails aborted!
NameError: uninitialized constant PurIssue
/Users/me/gits/rffvp/lib/sti_preload.rb:33:in `block in preload_sti'
/Users/me/gits/rffvp/lib/sti_preload.rb:31:in `each'
/Users/me/gits/rffvp/lib/sti_preload.rb:31:in `preload_sti'
/Users/me/gits/rffvp/lib/sti_preload.rb:13:in `descendants'
/Users/me/gits/rffvp/app/models/concerns/validateable.rb:10:in `block in <module:Validateable>'
/Users/me/gits/rffvp/app/models/pursuit.rb:68:in `include'
/Users/me/gits/rffvp/app/models/pursuit.rb:68:in `<class:Pursuit>'
/Users/me/gits/rffvp/app/models/pursuit.rb:54:in `<main>'
/Users/me/gits/rffvp/app/models/pur_issue.rb:1:in `<main>'
Tasks: TOP => zeitwerk:check
(See full trace by running task with --trace)
I saw the same thing trying to do an upgrade to rails 7. I believe it originates with this change: https://github.com/rails/rails/commit/ffae3bd8d69f9ed1ae185e960d7a38ec17118a4d
Effectively the change to invoke Class.descendants directly in the internal association callback methods exposes a more longstanding implicit issue with invoking Class.descendants at all during the autoload, as the StiPreload implementation may result in a problem where it circularly attempts to constantize the original class being autoloaded. I've added an issue in the Rails repo here https://github.com/rails/rails/issues/44252

How do I implement a decorator in my app?

I am trying to implement this in my app.
The article says that I must create a decorator - but it doesn't go into much detail about exactly how to do that. This is the code:
module CartDecorator
extend ActiveSupport::Concern
module InstanceMethods
def is_downloadable?
items = self.items.collect { |li| li[:variant].item }
items.all? { |i| i.is_downloadable }
end
def has_downloadable?
items = self.items.collect { |li| li[:variant].item }
items.any? { |i| i.is_downloadable }
end
end
end
Piggybak::Cart.send(:include, CartDecorator)
I am not sure if I should add that code to some model.rb (for what it's worth, I don't have a piggybak_cart.rb in my app/models/ folder).
I tried running rails g decorator Cart and that didn't work.
What I have done is put the above code in app/helpers/cart_helper.rb.
Then when I tried to run a rails g command (for something else), I am now getting this error:
/.rvm/gems/ruby-2.0.0-p0#myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize': uninitialized constant CartHelper (NameError)
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:136:in `block in modules_for_helpers'
from /.rvm/gems/ruby-2.0.0-p0#myapp/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:131:in `map!'
What's the best way to approach this?
You should add the above code into app/decorators/cart_decorator.rb
the decorators folder will be new and be autoloaded when you start your rails app. when this is run Piggybak::Cart.send(:include, CartDecorator) it will decorate your Piggybag::Cart with the methods you declared above.

Rails 3.2 Uniqueness validation raises undefined method 'zero?' for nil:Nilclass

I am using Rails 3.2.0.
I have a simple model as show below
class Favorite < ActiveRecord::Base
validates :lst, :presence => true
validates :uuid, :presence => true, :uniqueness => {:scope => :lst}
end
If I try this
f = Favorite.new
f.valid?
I get the following error message:
NoMethodError: undefined method `zero?' for nil:NilClass
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/associations/alias_tracker.rb:28:in `aliased_name_for'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/associations/join_dependency.rb:17:in `initialize'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:219:in `new'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:219:in `construct_join_dependency_for_association_find'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:192:in `exists?'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/validations/uniqueness.rb:32:in `validate_each'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validator.rb:153:in `block in validate'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validator.rb:150:in `each'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validator.rb:150:in `validate'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:310:in `_callback_before_15'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:429:in `_run__1275595979440079611__validate__42615372200132002__callbacks'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:405:in `__run_callback'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validations.rb:212:in `run_validations!'
from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
....
....
ActiveRecord AREL table aliasing is breaking
The error is likely due to ActiveRecord AREL losing track of how to sum an empty array.
The relevant line of code is in the file alias_tracker.rb:
count.sum
If count is an empty array then the line evaluates as:
[].sum
In Ruby that fails:
$ irb
> [].sum
NoMethodError: undefined method `sum' for []:Array
Rails ActiveSupport Enumerable#sum
In Rails that succeeds because ActiveSupport is creating Enumerable#sum
$ irb
> require 'active_support/core_ext/enumerable'
> [].sum
=> 0
Your problem is likely that some unrelated area of your app is also creating Enumerable#sum or Array#sum. The unrelated code is overwriting the Rails method.
The could be happening in your code or in an unrelated gem. The Rails gem loads early, typically first in your Gemfile, and any later gem can interfere with Rails.
How to fix it?
Have you written a method named sum, perhaps within a module named Enumerable or Array? If so, that's a good place to start. You could rename your method, or you could try changing your method to match the Rails method by replacing your #sum code with this code:
module Enumerable
def sum(identity = 0, &block)
if block_given?
map(&block).sum(identity)
else
inject(:+) || identity
end
end
end
If you haven't written a method named sum in your code, then the conflict is likely in a gem you're using. Try commenting-out gems that you're using then reload your app.
You can search for a gem that defines a method named sum like this:
$ cd /usr/lib/ruby/gems
$ find | xargs grep "def sum\b"
Are you using any gems named sixarm? If so, contact me and I'll fix them for you. These are by me, and a few of them do define #sum for use with statistics tools and utilties.
Hope this helps. Can you post here if it solves your issue?

Heroku [WARNING] You provided devise_for :users but there is no model User defined in your application

I have an rails 3 (3.0.9) application working on ruby 1.8.7 with the gem devise (1.4.2) on my computer working perfectly.
I tried to push it on heroku and i got the following error message on application load:
[WARNING] You provided devise_for :users but there is no model User defined in your application
=> Booting WEBrick
=> Rails 3.0.9 application starting in production on http://0.0.0.0:43292
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:124:in `block in constantize': uninitialized constant User (NameError)
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:123:in `each'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/inflector/methods.rb:123:in `constantize'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:84:in `to'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:79:in `modules'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise/mapping.rb:88:in `strategies'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:410:in `block in configure_warden!'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:409:in `each_value'
from /app/vendor/bundle/ruby/1.9.1/gems/devise-1.4.2/lib/devise.rb:409:in `configure_warden!'
The problem comes from devise but i don't know how to fix it.
My model User is correctly defined, and it works on my computer...
Does anyone know how to fix this ?
Thanks for you help
Make sure you have this:
class User < ActiveRecord::Base
Defined at the top of your user/devise model.
ALso, make sure you run your migrations.
heroku rake db:migrate
In console.
Check inside your users.rb to make sure that you spelled ':database_authenticable' correctly, this seems to be a common problem with this error.
class User < ActiveRecord::Base
devise :database_authenticable, :recoverable,
:rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
Also, I suggest you take a look here, to see how these people fixed the error.
http://groups.google.com/group/plataformatec-devise/browse_thread/thread/807f4c6e3475622f

ACL9 and Rails 2.3.8 - NameError: uninitialized constant User::Role

I am running RVM (Ruby 1.8.7-head, Rails 2.3.8) and have the BASICS of a new app running. I have Authlogic working, with the minimal code to make that work. I am attempting to use ACL9 (which I have working on a different project, same RVM gemset)
class User < ActiveRecord::Base
# authentication
acts_as_authentic
# authorization
acts_as_authorization_subject
def full_name
"#{self.first_name} #{self.last_name}"
end
end
When I attempt to just run a simple check to make sure ACL9 is working properly...
script/console
u = User.first
u.has_role?(:anyrole)
I get this error...
ruby-1.8.7-head > u.has_role?(:anyrole)
NameError: uninitialized constant User::Role
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in `const_missing'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/base.rb:1:in `compute_type'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activesupport-2.3.8/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/base.rb:2230:in `compute_type'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:156:in `send'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:156:in `klass'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:187:in `quoted_table_name'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/has_and_belongs_to_many_association.rb:102:in `construct_sql'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:21:in `initialize'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/has_and_belongs_to_many_association.rb:5:in `initialize'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations.rb:1306:in `new'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations.rb:1306:in `role_objects'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/acl9-0.12.0/lib/acl9/model_extensions/for_subject.rb:39:in `has_role?'
from (irb):2
I'm not sure why this works with every other app I have, but not this one - and I'm not sure what the error message is saying.. any help is appreciated.
class Role < ActiveRecord::Base
acts_as_authorization_role
end

Resources