Rails 4.2 concern content ignored in test environment - ruby-on-rails

I'm stuck in a really weird issue. I recently updated from rails 4.1 to 4.2. In 4.1 everything worked fine. But now, in 4.2, it seems model concern content is beig ignored, even if the concern is found.
Putting it simple. I have a Client model which belongs to an Address, relationship that is defined in an Addressable concern:
app/models/client.rb
class Client < ActiveRecord::Base
include Addressable
end
app/models/address.rb
class Address < ActiveRecord::Base
end
app/models/concerns/addressable.rb
module Addressable
extend ActiveSupport::Concern
included do
belongs_to :address
end
No problem in development environment. I can do Client.new.address without problems. But in test environment that same line gives me an unknow method address error. If I move belongs_to :address to Client then it works again. The module is being included, because there is no error about this, and if I change the name to Adressabledewdewded then, of course, there is an error.
Same happens with validation methods, etc.
I have been debugging this but I don't find the root. I have copied my development.rb as test.rb and restarted the server but still. I can't reproduce this in a freshly new Rails 4.2 application, so it has to have something to do with my application or the updating process, but no idea where.
Anyone has a clue?
Thanks
UPDATE
This is the stack trace on doing Client.new.address in the console in test environment (in development it works):
NoMethodError: undefined method `address' for #<Client:0xbc89dac0>
from /home/marc/.gem/ruby/2.1.3/gems/activemodel-4.2.0/lib/active_model/attribute_methods.rb:433:in `method_missing'
from (irb):3
from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/marc/.gem/ruby/2.1.3/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /home/marc/projects/clients_webs/a_project/bin/rails:8:in `<top (required)>'
from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/marc/.gem/ruby/2.1.3/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/commands/rails.rb:6:in `call'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/command_wrapper.rb:38:in `call'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:183:in `block in serve'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:156:in `fork'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:156:in `serve'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:131:in `block in run'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:125:in `loop'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application.rb:125:in `run'
from /home/marc/.gem/ruby/2.1.3/gems/spring-1.3.1/lib/spring/application/boot.rb:18:in `<top (required)>'
from /home/marc/.rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/marc/.rubies/ruby-2.1.3/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
UPDATE
Result of Client.ancestors in both environments is (without the only difference of instance identity, of course):
=> [Client (call 'Client.connection' to establish a connection), Trader, Addressable, Kaminari::ConfigurationMethods, Kaminari::ActiveRecordModelExtension, Client::GeneratedAssociationMethods, #<#<Class:0xba3b1c70>:0xba3b1d24>, ActiveRecord::Base, Kaminari::ActiveRecordExtension, GlobalID::Identification, ActiveRecord::Store, ActiveRecord::Serialization, ActiveModel::Serializers::Xml, ActiveModel::Serializers::JSON, ActiveModel::Serialization, ActiveRecord::Reflection, ActiveRecord::NoTouching, ActiveRecord::Transactions, ActiveRecord::Aggregations, ActiveRecord::NestedAttributes, ActiveRecord::AutosaveAssociation, ActiveModel::SecurePassword, ActiveRecord::Associations, ActiveRecord::Timestamp, ActiveModel::Validations::Callbacks, ActiveRecord::Callbacks, ActiveRecord::AttributeMethods::Serialization, ActiveRecord::AttributeMethods::Dirty, ActiveModel::Dirty, ActiveRecord::AttributeMethods::TimeZoneConversion, ActiveRecord::AttributeMethods::PrimaryKey, ActiveRecord::AttributeMethods::Query, ActiveRecord::AttributeMethods::BeforeTypeCast, ActiveRecord::AttributeMethods::Write, ActiveRecord::AttributeMethods::Read, ActiveRecord::Base::GeneratedAssociationMethods, #<#<Class:0xb8df1c14>:0xb8df1c50>, ActiveRecord::AttributeMethods, ActiveModel::AttributeMethods, ActiveRecord::Locking::Pessimistic, ActiveRecord::Locking::Optimistic, ActiveRecord::AttributeDecorators, ActiveRecord::Attributes, ActiveRecord::CounterCache, ActiveRecord::Validations, ActiveModel::Validations::HelperMethods, ActiveSupport::Callbacks, ActiveModel::Validations, ActiveRecord::Integration, ActiveModel::Conversion, ActiveRecord::AttributeAssignment, ActiveModel::ForbiddenAttributesProtection, ActiveRecord::Sanitization, ActiveRecord::Scoping::Named, ActiveRecord::Scoping::Default, ActiveRecord::Scoping, ActiveRecord::Inheritance, ActiveRecord::ModelSchema, ActiveRecord::ReadonlyAttributes, ActiveRecord::Persistence, ActiveRecord::Core, Object, PP::ObjectMixin, ActiveSupport::Dependencies::Loadable, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
And doing Client.reflections.keys makes address to appear in development but not in test.

I got it. The thing is that a gem that is present only in my test environment (json-schema), has addressable gem as a dependency, which is defining an Addressable module. When Rails tries to autoload Addressable it finds the class defined by addressable instead of my concern class. The module is being included but it is not the one I thought.
When updating my Gemfile, json-schema has been updated and now depends on addresable, since November 2014.
Hard one to catch :)

Related

Rails losing inheritance of Application controller

After conducting a set of changes to migration, dropping, re-creating and, migrating and loading data, while Unit test are running and passing as expected, controller tests have stopped running and fail because of:
uninitialized constant ApplicationController (NameError)
class MunicipalsController < ApplicationController
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/bootsnap-1.12.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/bootsnap-1.12.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/railties-7.0.3/lib/rails/test_unit/runner.rb:47:in `block in load_tests'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/railties-7.0.3/lib/rails/test_unit/runner.rb:47:in `each'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/railties-7.0.3/lib/rails/test_unit/runner.rb:47:in `load_tests'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/railties-7.0.3/lib/rails/test_unit/runner.rb:40:in `run'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/railties-7.0.3/lib/rails/commands/test/test_command.rb:33:in `perform'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/railties-7.0.3/lib/rails/command/base.rb:87:in `perform'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/railties-7.0.3/lib/rails/command.rb:48:in `invoke'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/railties-7.0.3/lib/rails/commands.rb:18:in `<main>'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/bootsnap-1.12.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
from /Users/main/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/bootsnap-1.12.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
It is as if the application see the app/controllers/application_controller.rb file - which was unaltered by the changes -
class ApplicationController < ActionController::Base
include UniversalMethods
as having a different name. Possible corruption?
What can be done to get the process running anew?
Note: the application is running as expected, based on preliminary observations...
Try closing all your workers and stopping spring with spring stop.
If that doesn't work, you can try restarting your terminal.

my_zipcode_gem migration generators not working

I'm pretty sure that the problem is due to the fact that this gem looks to be 3-4 years old and rails has changed how we do migrations. But I'm not too familiar with gems/generators. I'm trying to follow the instructions to do the instructions listed here.
rails g my_zipcode_gem:models
rake db:migrate
rake zipcodes:update
However, when I do the first step I end up getting this:
/Users/thammond/.rvm/gems/ruby-2.1.1/gems/my_zipcode_gem-0.1.3/lib/generators/my_zipcode_gem/models_generator.rb:35:in `create_migration': wrong number of arguments (3 for 0) (ArgumentError)
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/generators/migration.rb:63:in `migration_template'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/my_zipcode_gem-0.1.3/lib/generators/my_zipcode_gem/models_generator.rb:36:in `create_migration'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/generators.rb:157:in `invoke'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/generate.rb:11:in `<top (required)>'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `block in require'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:135:in `generate_or_destroy'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:51:in `generate'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/thammond/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Any idea what I need to do to get that gem working? It provides exactly what I'm looking for in my app.
Thanks!
Edit: Adding the generation code:
module MyZipcodeGem
class ModelsGenerator < Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
def initialize(*args, &block)
super
end
def generate_models
# puts ">>> generate_zipcodes:"
end
def add_gems
add_gem "mocha", :group => :test
end
def create_models
template 'zipcode_model.rb', "app/models/zipcode.rb"
template 'county_model.rb', "app/models/county.rb"
template 'state_model.rb', "app/models/state.rb"
end
# Implement the required interface for Rails::Generators::Migration.
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end
def create_migration
migration_template 'migration.rb', "db/migrate/create_my_zipcode_gem_models.rb"
end
def create_rakefile
template 'zipcodes.rake', "lib/tasks/zipcodes.rake"
end
end
end
# /Users/cblackburn/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/rails/generators/active_record/
It looks like it is calling the migration using parameters that are no longer expected. You have two options:
1) fork the code, update it to use rails 4 conventions, and create a pull request on github. Assuming the owner is still active they will bring your code into the master and republish the gem. You would have help support the community and feel good about it in the process. There are plenty on here like myself who can help you with this.
2) Find another gem to do what you want, such as http://www.rubygeocoder.com/ which I've used several times and is awesome at doing location/zipcode problems

Rails is throwing an error the first time an attribute is assigned to my model

I have a rails model that keeps track of other models in an app. (unusual, but necessary for this project).
When I try to assign the attribute model_name I get an error that occurs once, but then doesn't occur again.
AppModel
model_name created_at updated_at
----------------------------------------
User
Question
Post
Answer
In console, I'm getting a strange error:
> model = AppModel.new
> model.model_name = "User" # First time causes the error.
> model.model_name = "User" # Second time it works.
Error:
ActiveRecord::DangerousAttributeError: record_changed? is defined by Active Record
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:104:in `instance_method_already_implemented?'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:288:in `block in define_attribute_method'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:285:in `each'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:285:in `define_attribute_method'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `block in define_attribute_methods'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `each'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:252:in `define_attribute_methods'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:75:in `block in define_attribute_methods'
from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mutex_m.rb:73:in `synchronize'
from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/mutex_m.rb:73:in `mu_synchronize'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:72:in `define_attribute_methods'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:196:in `method_missing'
from (irb):3
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
from /Users/donaldpinkus/Projects/hooker/bin/rails:8:in `<top (required)>'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/donaldpinkus/.rvm/gems/ruby-2.1.5/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/donaldpinkus/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'2.1.5 :004 > e.model_name = "User"
=> "User"
"model_name" isn't a reserved word for models, so what is happening here?
This happens because there is already a method in ActiveRecord models, which is called exactly model_name, that returns (obviously) the name of this model (among other things).
From:
.../ruby-2.1.5/gems/activemodel-4.2.0/lib/active_model/naming.rb
# line 222:
Owner: ActiveModel::Naming
Visibility: public
Signature: model_name()
Number of lines: 12
Returns an ActiveModel::Name object for module. It can be used to
retrieve all kinds of naming-related information (See
ActiveModel::Name for more information).
When ActiveRecord fetches an object from the table, it also creates methods with names corresponding to the column names in the table. What happens is it fetches an object, sees a column model_name on the given object, tries to define a method model_name and suddenly finds out that it already exists.
Here's a thing: avoid needlessly long attribute names. If you're defining a name in a model, it can just be name, it's scoped to "model" class anyway, obviously it's the model's name.
The error reporting code, however, likely contains a bug that needs to be tracked down.

Rails monkey patch only on certain columns

I am trying to include a module only if the subclass has a certain column,
and I am doing this in an initializer:
class ActiveRecord::Base
def self.inherited(subclass)
subclass.include(MultiTenancy) if subclass.new.respond_to?(:tenant_id)
end
end
I keep getting this error:
NoMethodError: undefined method `[]' for nil:NilClass
Here is the module that I am importing:
module MultiTenancy
class TenantNotSetError < StandardError ; end
def self.included(model)
model.class_eval do
belongs_to :tenant
validates :tenant_id, presence: true
default_scope -> {
raise TenantNotSetError.new unless Tenant.current_tenant
where(tenant_id: Tenant.current_tenant.id)
}
def multi_tenanted?
true
end
end
end
end
What am I doing wrong. I can include the module on any subclass separate eg. Klass.include(MultiTenancy) successfully, so the problem is with the initializer.
Below is the stack trace:
from -e:1:in `<main>'2.1.1 :002 > Klass.all
NoMethodError: undefined method `[]' for nil:NilClass
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.4/lib/active_record/relation/delegation.rb:9:in `relation_delegate_class'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.4/lib/active_record/relation/delegation.rb:112:in `relation_class_for'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.4/lib/active_record/relation/delegation.rb:106:in `create'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.4/lib/active_record/model_schema.rb:133:in `table_name='
from /home/lee/Code/mobifit/app/models/klass.rb:25:in `<class:Klass>'
from /home/lee/Code/mobifit/app/models/klass.rb:22:in `<top (required)>'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:443:in `load'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:443:in `block in load_file'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:633:in `new_constants_in'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:442:in `load_file'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:342:in `require_or_load'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:480:in `load_missing_constant'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:180:in `const_missing'
from (irb):2
from /home/lee/.rvm/gems/ruby-2.1.1/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
from /home/lee/Code/mobifit/bin/rails:8:in `<top (required)>'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `block in load'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/lee/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
from /home/lee/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/lee/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.1.1 :003 >
If you follow the stack trace back a bit, you can see that the thing that's nil that isn't supposed to be is is the class's #relation_delegate_cache. Looking at the source, you can see that this normally gets initialized in the initialize_relation_delegate_cache method, which is called in the inherited hook for ActiveRecord::Delegation::DelegateCache.
ActiveRecord::Base extends this module, so normally a class that inherits from ActiveRecord::Base would call this inherited hook. So the problem with your code is that you're monkey-patching inherited on ActiveRecord::Base, which means that the hook in ActiveRecord::Delegation::DelegateCache is no longer being called.
Two comments:
This is why it's a bad idea to monkey-patch, especially something as fundamental and complex as active record. Is it possible to do what you want to do in a more straightforward way?
If it's not, you should take some notes on how ActiveRecord::Delegation::DelegateCache works and rewrite your monkey patch to mix in a module of your own, rather than just over-writing ActiveRecord::Base's inherited method.
The suggestion in (2) would look something like this:
module MultitenancyConcern
def self.inherited(subclass)
subclass.include(MultiTenancy) if subclass.column_names.include?("tenant_id")
super #this is critical for avoiding the error you're getting
end
end
then your monkey-patch becomes just
ActiveRecord::Base.extend(MultitenancyConcern)

including module works in development, fails at startup in production?

I've authored a module that lives in my lib folder. I'm including it at startup by putting it in my environment.rb file as follows
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
MyProject::Application.initialize!
ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters
This works great in development but if I try to run in production mode or if I deploy my app to production I get the following stack trace when the server tries to start up:
=> 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
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-3.0.3/lib/active_record/base.rb:1008:in `method_missing': undefined local variable or method `has_formatter' for #<Class:0x808f830> (NameError)
from C:/Data/myproject/app/models/event.rb:2
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:227:in `load_dependency'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:346:in `require_or_load'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:300:in `depend_on'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:216:in `require_dependency'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:138:in `eager_load!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `each'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:137:in `eager_load!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `each'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/engine.rb:135:in `eager_load!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:108:in `eager_load!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application/finisher.rb:41
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `run_initializers'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `send'
from C:/Ruby187/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
from C:/Data/myproject/config/environment.rb:5
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:596:in `new_constants_in'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:225:in `load_dependency'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-3.0.3/lib/active_support/dependencies.rb:239:in `require'
from C:/Data/myproject/config.ru:3
from C:/Ruby187/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
from C:/Ruby187/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
from C:/Data/myproject/config.ru:1:in `new'
from C:/Data/myproject/config.ru:1
Now here is the most mind boggling part of the whole thing: If I set config.cache_classes = false in config/environments/production.rb then I don't get that stack trace and the app behaves the same in production mode as it does dev mode!!
What about settings config.cache_classes = false makes including a module in this fashion work in production? Am I including the module in a wrong/weird way?
Hope I'm totally, right, I'll at least present my point of view:
In development, all your classes are lazy loaded. This way, you can easily change them since they're loaded only when necessary. This means that:
MyProject::Application.initialize!
does not load module content which requires:
ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters
which is included afterwards, it makes the whole thing working.
In production, modules and classes are really loaded => crash, they need their dependencies!
As a result, put:
ActiveRecord::Base.send :include, MyProject::Has::Formatter
include MyProject::EventFormatters
in an initializer you create in config/initializers, call it active_record_extension.rb for instance.
Does autoloading not work for your scenario?
# config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
# lib/my_project/event_formatters.rb
module MyProject
module EventFormatters
# .. etc. ...
end
end

Resources