'load_missing_constant' when trying to create an observer on Rails - ruby-on-rails

I am trying to create an observer for my Offer model but I keep getting this error:
/Users/codus/.rvm/gems/ruby-1.9.3-p194#gyp/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected /Users/codus/Projetos/gyp-revolution/app/models/offer.rb to define Offer (LoadError)
There is nothing special about my model and I am sure it is declared in the right place (the application works fine without the observer).
I've generated my observer with the Rails script
rails g observer offer
And I added this in my config/application.rb file
config.active_record.observers = :offer_observer
The strangest part is that it works fine with all my other models.
I am using Rails 3.2.6 with Ruby 1.9.3p194.

The problem was that I was using FactoryGirl, and inside my Offer factory I called a constant defined in my model. The factory was being loaded before the model, so this error was happening.
It's weird that the observer changed this, it works fine without it. Now I just use the value in my factory, not the constant.

Related

Rails query object not loading

I have a query object in Rails 5.2.2
app/queries/car_query.rb
class CarQuery
attr_reader :relation
# code
end
when I reference it in the console, I get
Error
NameError (uninitialized constant CarQuery)
Since the folder/file is in the app directory, I thought it automatically gets loaded. Am I incorrect? If so, why would my form objects autoload and not these? How do I fix this?
Try running bin/spring stop in your console then restarting the app.
If that doesn't work add update you application.rb with
Spring.watch "app/queries/**"
Make sure that the name of the file corresponds to the class name, or else rails will have trouble loading it. That is to say, make sure the file name is car_query.rb, if CarQuery is the name of the class.
CarQuery.rb goes against rails naming conventions and will mess with autoloading.

Rails: Why is my model nil even though my migration creates the table?

I recently decided to change the name of a model. So I dropped the DB calling rails db:drop.
Using sublime text editor I did a find & replace for the model name, and I renamed all relevant directories to be in line with convention; including the migration file etc.
I deleted the schema file.
I called rails db:create to create the DB: no errors
I called rails db:migrate to create the tables and schema: no errors.
When I try to call a method on the model class... ('Set')
> Set.all
NoMethodError: undefined method `all' for Set:Class
from (pry):4:in `<main>'
For a test, I ran rails generate model Set and got the following:
Running via Spring preloader in process 2042
invoke active_record
The name 'Set' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
So here it seems the model does exist. But it doesn't exist in console.
Why is my model nil
It's not. It's unclear where you drew this conclusion from.
NoMethodError: undefined method `all' for Set:Class
Name of your model (Set) is the same as class from the standard lib. Due to how rails' autoloading works, name Set is resolved to the stdlib class, not your model. Rename your model with a non-existing name (just as the warning suggests).

Install generator won't see a method I defined

I'm trying to use my Engine in another app to test the install generator and it seems to be failing. I haven't used my install generator for a long time, so I'm not sure when it broke (or if it ever truly smoothly worked). My project is based off radar/forem, so I tried to borrow a lot of their code (including the generator).
Edit: My installer works for the engines test/dummy but not in other apps. Why?
GH issue: https://github.com/NJayDevelopment/mongoid_forums/issues/16
Here is the log:
$ rails g mongoid_forums:install
What is your user class called? [User]
What is the current_user helper called in your app? [current_user]
Defining mongoid_forums_user method inside ApplicationController...
insert app/controllers/application_controller.rb
Adding mongoid_forums initializer (config/initializers/mongoid_forums.rb)...
create config/initializers/mongoid_forums.rb
(erb):5:in `template': undefined method `per_page' for MongoidForums:Module (NoMethodError)
The route is successfully added, however the initializer/mongoid_forums.rb is a blank file. The method is defined exactly how radar/forem does it, what could be the error?
Here is the relevant code:
Per page method definition: https://github.com/NJayDevelopment/mongoid_forums/blob/master/lib/mongoid_forums.rb#L33
Mattr accessor:
https://github.com/NJayDevelopment/mongoid_forums/blob/master/lib/mongoid_forums.rb#L9
Initializer template:
https://github.com/NJayDevelopment/mongoid_forums/blob/master/lib/generators/mongoid_forums/install/templates/initializer.rb
Install generator at error point:
https://github.com/NJayDevelopment/mongoid_forums/blob/master/lib/generators/mongoid_forums/install_generator.rb#L47
Turns out when you try requiring mongoid_forums in pry, you'll see that an error involving decorators occurs. The issue is fixed here in my pull request to decorators: parndt/decorators#13
It's because of the way files are required and how load! is called over there.
Waiting on PR status, that is the same version that radar/forem uses as well.

Conflict between Rails Admin and Impressionist gems

I'm using the latest version of the Impressionist and Rails Admin gems, and wondering if anyone could shed some light on an annoying conflict I'm experiencing. The problem is roughly documented here - https://github.com/sferik/rails_admin/issues/1315, yet the vaguely described solution is not working for me. When I have the line is_impressionable in my Listing model, I get an error when starting my Rails server with rails s:
...rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined local variable or method `is_impressionable' for Listing(no database connection):Class (NameError)
If I first start the server, and then add the 'is_impressionable' line, everything works fine, so the problem only occurs during initialization. I don't fully understand the initialization process, so am not sure how to go about getting this to work.
I have tried moving all my rails_admin model configuration options to their respective models, rather than in the initializer, which had no effect. I also have the following line in my initializer:
config.included_models = [Listing,ListingImage,AllOtherModelsHere...]
I have tried adding single quotes around these model names, which results in the following errors, as described in the github issue here
[RailsAdmin] Could not load model Listing, assuming model is non existing. (undefined local variable or method `is_impressionable' for Listing(no database connection):Class)
Any ideas what else I can try to make these gems work together? I don't want to have to remove the is_impressionable line every time I want to restart the server or generate a migration...
Not sure if the same issue that I had but yet I will post what worked for me just in case someone struggles with this too:
Im on a ruby 2.1.5 project with rails 4.2.0 and among other gems I'm using rails admin.
I run into several weird problems trying to set this up. For instance if I added the is_impressionable call within one of my models for some reason the execution of that file stopped there and I started getting weird errors like any method declared below the is_impressionable failed with undefined error.
So what I end up doing was:
class MyModel < ActiveRecord::Base
include Impressionist::IsImpressionable
is_impressionable
end
So this solved my issue and now i can access #my_model_instance.impression_count as expected.
I changed every occurrence of Klass to 'Klass'.constantize in initializer.

Having default Rails generators call a custom generator

To be clear, here's NOT what I'm trying to:
Have my custom generator call a default Rails generator
Replace a default Rails generator with my own
What I want to do is have my generator be invoked automatically when I call:
rails generate scaffold User name age:integer
I'm not writing a test replacement or anything, it's completely custom. All information I find about generators out there involve one of those first two cases but not what I want to do. As soon as I found hook_for I immediately thought that was exactly what I needed, but it appears to do the opposite -- invokes another Rails generator from inside of my custom one (if I wanted a test file created for my custom generator I'd call hook_for :test_framework and then define a TestUnit::MyCustomGenerator class somewhere).
I suppose I could monkey patch the default scaffold generator to call mine but that feels dirty. I've looked into some gems that do something similar like https://github.com/Skalar/i18n-yaml-generator but trying to convert that to use an initializer and lib/generators isn't working for me. The scaffold_generator runs but mine never gets called.
for me it works from lib/generators/
$ rails g generator scaffold
create lib/generators/scaffold
create lib/generators/scaffold/scaffold_generator.rb
create lib/generators/scaffold/USAGE
create lib/generators/scaffold/templates
$ rails g scaffold
Usage:
rails generate scaffold NAME [options]
....
what/will/it/create
http://guides.rubyonrails.org/generators.html#generators-lookup
another way may be :)
fork railties gem
override Rails::Generators::ScaffoldGenerator class to your liking
install locally or specify source path in Gemfile
also if 'its completely custom', it is just fair to call it a different name, no?

Resources