how to solve undefined local variable or method `has_mailbox'? - ruby

I am following this tutorial to add messaging functionality between the users in my rails app, but got an error in my User model.
class User < ActiveRecord::Base
has_mailbox
end
This gives me the following error:
undefined local variable or method `has_mailbox' for #<Class:0xb60f6f84>
Any ideas?

I've checked this gem, try to run
rails g has_mailbox:install
when you install any gem in rails app, the good idea is to check generators available
rails g //list of available generators

Related

Why 'ApplicationRecord.descendants.count' return only 1 but In my rails Application there are many inheriting models of ApplicationRecord?

I ran following code in rails console:
ApplicationRecord.descendants.count # It returns only 1
Even there are many inheriting classes from the "ApplicationRecord" are available inside project_root/app/models/ in rails 5.2.4.
The models won't be loaded when you open the console, you can use #eager_load! after opening the rails console to change that:
Rails.application.eager_load!

NameError: undefined local variable or method `broadcast_messages' for #<RailsStdoutLogging::StdoutLogger:0x007f4d099fb6b0>

Title error when accessing ActiveRecord model only on heroku with rails 5.0.0-beta1. Works on production env heroku. Problem persists anytime Rails.logger is set to STDOUT wether between gem 'rails_stdout_logging' or in config/environment/production.rb
I've reviewed implementation and see broadcast_messages as a method created by the accessor macro. I'm at a loss and am looking for suggestions to troubleshoot.
Turns out heroku's rails_stdout_logging gem which is included as part of heroku's rails_12factor gem has a bug with rails 5.0.0-beta1.
StdoutLogger inherits from stdlib's Logger rather than ActiveSupport's Logger via < ::Logger. I've got a workaround on my fork but it injects an ActiveSupport dependency when not using rails. I'm new to ruby so I don't know best practice to make a more eloquent fix.
See https://github.com/MattWalston/rails_stdout_logging.

Models on the Rails Console

I have created a simple class under the models folder like so:
class CaffeineShops
def initialize
end
end
When I run rails console and try CaffeineShops.new(), I get a message: "NoMethodError: undefined method 'new' for CaffeineShops::Module"
I am using Rails 4.1.
Any ideas why I am getting that error?
You cannot name a model the same name as the application itself. When you run rails g caffeine_shops this creates a module named CaffeineShops which will power your application.
The module is utilized in many files including
config\application.rb
config\environment.rb
config\environments\development.rb
config\environments\production.rb
config\environments\test.rb
config\initializers\secret_token.rb
config\initializers\session_store.rb
config\routes.rb
config.ru *actually starts the application on Rack-based servers
Rakefile *loads rake tasks for the application
When you then try to name a class the same name it creates ambiguity in the rails application so your code is assuming you mean the module CaffeineShops not the class CaffeineShops.
If you were to actually use a generator to define this model rails would make it very clear there was a problem with this e.g.
rails g caffeine_shops
cd caffeine_shops
rails g model caffeine_shops
#=>The name 'CaffeineShops' is either already used in your application or reserved
by Ruby on Rails. Please choose an alternative and run this generator again.

Ruby Gems Documentation

I'm just trying to understand how to use particular ruby gems. For example, take this reddit gem. It says to have this code to start:
require 'snoo'
# Create a new instance of the client
reddit = Snoo::Client.new
# Log into reddit
reddit.log_in 'Username', 'Password'
# Send a private message to me (Paradox!)
reddit.send_pm 'Paradox', 'Snoo rubygem rocks!', "Hey Paradox, I'm trying your Snoo rubygem out and it rocks. Thanks for providing such an awesome thing!"
# Log back out of reddit
reddit.log_out
Great but in the documentation you can see that the Client class doesn't have very many exciting functions. The exciting functions are in the Account class but there is no way to get to it...because if I try something like this
reddit = Snoo::Account.new
I get this error:
`initialize': undefined method `new' for Snoo::Account:Module (NoMethodError)
Okay so there's no new method but how do I make an Account object and use its functions like log_in?
Snoo::Account is a Ruby Module, and has been mixed in to Snoo::Client already by the gem. All the functions of Snoo::Account are already available to you on the reddit object.
The synopsis documentation in the readme doesn't make this very clear. But otherwise the documentation on the gem looks good to me.
Taking a short look at the source code on github makes me believe this is a fault in the documentation, as client clearly includes the functionality of many other modules, including the Account module you would like to access. In your example code, try the following methods to confirm it for yourself:
reddit.methods.sort
reddit.is_a? Snoo::Account
I assume the documentation software didn't catch the includes as they were executed using a block.

Rails 3.1 Engine plugin rspec testing (external gem Spree Commerce)

I created an engine inside an app that depends on an external gem: Spree.
And I did my best to set RSpec within that engine following this guide.
When I try testing a controller with some integrated testing:
tests_spec.rb:
require 'spec_helper'
describe "Tests" do
describe "GET /tests" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get tests_path
response.status.should be(200)
end
end
end
I get an error in the dummy app's environment.rb file, that says that following command couldn't be run:
# Initialize the rails application
Dummy::Application.initialize!
Because my controller is using Admin::ResourceController as an extension which is declared within the external Spree gem. As if rspec is unable to read that class from gemfile of the main app (the one that contains the engine plugin).
Can someone please shed some light on that?

Resources