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.
Related
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.
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.
Why am I getting this error?
https://gist.github.com/770219
Your LegalDocument model may be defined in a filename that is not correct. The file must be located in any subdirectory of app and be called legal_document.rb. The name is really important, as this is how Rails does autoloading.
Is there a way to have rails raise an error if an attempt is made to mass-assign attributes that aren't allowed by attr_accessible?
This would be handy in development to remind me why my shiny new model isn't working, and also good to log in production in order to detect malicious activity.
I'm using rails 2.3.8 but will probably soon be migrating to 3.
As of Rails 3.2 this no longer requires monkeypatching -- rails provides this behavior now. Put this in development.rb and test.rb:
config.active_record.mass_assignment_sanitizer = :strict
I would suggest something like the Bento project has incorporated into their Rails app.
They create a Rails Initializer under config/initializers/ and then override the appropriate method in the ActiveModel class to raise a MassAssignmentError (within non-production environments).
I am not sure if this would work, but you could write a test to see if your object "respond_to(:unexpected_attr)". You can then tried to force feed it that attr
Alex
I am trying to get the OAuth gem to work with Rails 3 and I'm running into this weird problem... (independent of the gem, I think I've run into this once before)
I have a controller called "OauthTestController", and a model called "ConsumerToken". The model looks like this.
require 'oauth/models/consumers/token'
class ConsumerToken < ActiveRecord::Base
include Oauth::Models::Consumers::Token
end
When I go to "/oauth_test/twitter", it loads the Oauth::Models::Consumers::Token module and I'm able to connect to twitter no problem. But the second time I try it (just refresh the /oauth_test/twitter url), it gives me this error:
NameError (uninitialized constant Oauth):
app/models/consumer_token.rb:4
app/models/twitter_token.rb:2
app/controllers/oauth_test_controller.rb:66:in `load_consumer'
Why is that? It has something to do with load paths or being in development mode maybe?
Try using require_or_load instead of require. That forces full load each time when in development and can sometimes help with this sort of issue.
Yeah it's something to do with being in development mode. Setting config.cache_classes = true in your development.rb get's it working (but is a pain in the ass)