Rails NoMethodError: undefined method `method_name' for #<Object> - ruby-on-rails

I am getting undefined method 'xyz' for #. 'xyz' is a instance method written inside ABC Class. Calling the method using delayed job although the object exists. Why I am getting this error. Please anyone help me.
Code Snippet:
device_obj.delay(run_at: 5.minutes.from_now).get_device_battery_status
In Device model:
def get_device_battery_status # Used in delayed Job
command_data = {"mode"=>"get_battery"}
self.send_command(command_data)
end

Related

in `_main': undefined method `run' for nil:NilClass (NoMethodError)

I can't call the run method in a class called MySqliteRequest. When I call the method,the error is going out.
in `main': undefined method `run' for nil:NilClass (NoMethodError)
Here some methods of MySqliteRequest
class MySqliteRequest
def print
puts "Type Of Request #{#type_of_request}"
end
def run
print
end
def _setTypeOfRequest(new_type)
if(#type_of_request == :none or #type_of_request == new_type)
#type_of_request = new_type
else
raise "Invalid: type of request already set #{#type_of_request} (new type => #{new_type}"
end
end
end
And main method ↓
def _main()
request = MySqliteRequest.new
request = request.from('nba_player_data.csv')
request = request.select('name')
request = request.where('birth_state', 'Indiana')
request.run
end
_main()
At the point where you call request.run the value for request is nil. This is why you are seeing the error you're given.
This is happening because the line right above it assigns the nil value to the request variable.
You are clearly coming from another language that is not Ruby (some type of C maybe?), by how you've formatted things. It would help for you to get more familiar with Ruby and its idioms. However, best I can tell, you want to do something like this instead:
def _main
request = MySqliteRequest.new
request.from('nba_player_data.csv')
request.select('name')
request.where('birth_state', 'Indiana')
request.run
end
_main()
This assumes you've also defined (and in some cases probably overridden) these methods on your MySqliteRequest Object or Model:
from
select
where
However, please note that the way you're going about this is just completely against how Ruby and Ruby on Rails is designed to work.

STI wth Strong params undefined local variable or method `attributes'

undefined local variable or method `attributes' for #CarsController:0x00007fa686991b30
def cars_params(type)
params.require(type.to_sym).permit(attributes)
end
My model is CommonCar::RedTrunk, so type has become "common_car_red_trunk"
I am using STI and following this.. https://gist.github.com/danielpuglisi/3c679531672a76cb9a91
I don't really understand why attributes was used and why its now failing. I assumed it maybe took the attributes from the required model, but not sure. Any insights would be helpful to get this working.

Rails, RSpec: How to test, that a specific mailer action gets triggered

I need to ensure that running an importer results in sending out an email.
This is what I got so far:
describe '#import' do
it 'triggers the correct mailer and action', :vcr do
expect(OrderMailer).to receive(:delivery_confirmation).with(order)
Importer.new(#file).import
remove_backed_up_file
end
end
It fails with:
pry(#<ActiveRecord::ConnectionAdapters::TransactionManager>)> error
=> #<NoMethodError: undefined method `deliver_now' for nil:NilClass>
Which obviously can't work out as I am expecting the Mailer class to receive the (instance) method call. But how can I get a hold of the mailer instance that will receive the call? How would you test that a unit's method triggers a certain mailer?
I assume the delivery_confirmation method in reality returns a Mail object. The problem is that ActionMailer will call the deliver method of the mail object. You've set an expectation stubbing out the delivery_confirmation method but you haven't specified what should be the return value. Try this
mail_mock = double(deliver: true)
# or mail_mock = double(deliver_now: true)
expect(mail_mock).to receive(:deliver)
# or expect(mail_mock).to receive(:deliver_now)
allow(OrderMailer).to receive(:delivery_confirmation).with(order).and_return(mail_mock)
# the rest of your test code
If I got you right,
expect_any_instance_of(OrderMailer).to receive(:delivery_confirmation).with(order)
will test the mailer instance that will receive the call.
For more precision you may want to set up your test with the particular instance of OrderMailer (let's say order_mailer) and write your expectation the following way
expect(order_mailer).to receive(:delivery_confirmation).with(order)

ActionMailer -- NoMethodError: undefined method X for MyMailer:Class

Question: Why is the method undefined if it's just there?
Details:
I have a very simple mailer class:
class ProductMailer < ApplicationMailer
def sample_email
mail(to: "me#example.com") # I hardcode my own email just to test
end
end
And a very simple call from ProductsController:
def sample_email
ProductMailer.sample_email().deliver_later
redirect_to #product, notice: 'Email was queued.'
end
The email fails to be sent. I am using Sidekiq to process emails in background. The Sidekiq Web UI shows failed jobs in the Tries page and I can see why it failed:
NoMethodError: undefined method `sample_email' for ProductMailer:Class
I tried to rename the method and restart the server with rails server but none of that removes the error. I am not using any namespaces.
Question: Why is the method undefined if it's just there?
Note: I found out by chance that the method is found if I name it notifybut maybe that's because I'm overwriting some method from ActionMailer base class, I don't know.
Answer: Restart Sidekiq
I created the mailer class before starting Sidekiq, but I renamed the sample_email method while Sidekiq was already running, so it seems that Sidekiq doesn't recognize new methods on-the-fly.
I renamed the method because I am used to development environment, where you can change almost anything on the fly...
It's because you've defined an instance method, and then you try to call it on a class. Change it to
def self.sample_email
....

Rails : Undefined Method in models

I have the following code in my model for a method to check if a particular email address exists for a branch:
def does_email_exist(email, branch_id)
if Person.for_branch(branch_id).where(:email => email).count == 0
return true
else
return false
end
end
However, when I call it from the Rails Console, I get the following error :
NoMethodError: undefined method `does_email_exist' for #<Class:0x007fdcb9fb8ab8>from
/Users/mkv/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.1.6/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
I have reloaded the console after adding the method.
This seems more like a class method than an instance method, try renaming your method to
def self.does_email_exist(email, branch_id)
Person.for_branch(branch_id).where(email: email).exists?
end
And yea I trimmed the method a bit, didn't need to be that long.

Resources