I am trying to create a method that loops through some objects and if a certain attribute is true, adds to the cost of the lesson to the money received, but keep getting the error undefined method `+' for nil:NilClass.
Here is the piece of code (the line producing the error start #activity.update_attribute):
def show
#activity = Activity.find(params[:id])
#participants = Participant.all.where(:activity_id => #activity.id).map(&:user_id).uniq
#all_participants = Participant.all.where(:activity_id => #activity.id)
#all_participants.each do |a_participant|
if a_participant.paid
#activity.update_attribute(:money_received, #activity.money_received + #activity.cost)
end
end
#users = Array.new
#participants.each do |participant|
#users.push(User.find(participant))
end
end
And here is a screenshot from my database to show that neither is a null value:
Here is the error message that I get when running the application:
Any help would be greatly appreciated
Many thanks
Related
I want to test a method that update promo_code to used if shopping_process is finalized. Additional it should create a pdf using ShoppingProcessDocumentCreatorFetcher
Here is my method which I want to test
def finalize_shopping_process(form)
if finalize_process == true
shopping_process.campaign_code.update(state: 'used')
document_creator_class = ShoppingProcessDocumentCreatorFetcher.new(shopping_process).call
document_creator_class.new(shopping_process).call
end
Success(form)
end
and the specs:
describe 'campain code' do
subject(:process_update) do
described_class.new(
shopping_process: shopping_process,
form: form,
finalize_process: true,
).call
end
it 'updates state of assigned campain code' do
updated_shopping_process = process_update.value!
expect(updated_shopping_process.campaign_code.state).to eq('used')
end
end
At the end I've got an error
NoMethodError:
undefined method `shopping_element_relations' for nil:NilClass
The weird thing is that if I remove this lines from the tested method:
document_creator_class = ShoppingProcessDocumentCreatorFetcher.new(shopping_process).call
document_creator_class.new(shopping_process).call
Specs will pas. I've no clue where I'm wrong.
Edit:
all error below:
ShoppingProcesses::Update.call campain code updates state of assigned campain code
Failure/Error: return false unless parent_group.shopping_element_relations.hiding.any?
NoMethodError:
undefined method `shopping_element_relations' for nil:NilClass
def finalize_shopping_process(form)
if finalize_process == true
shopping_process.campaign_code.update(state: 'used')
document_creator_class = ShoppingProcessDocumentCreatorFetcher.new(shopping_process).call
document_creator_class.new(shopping_process).call
end
Success(form)
end
In the above function document_creator_class this is initialized to the call method and not the class as intended.
Replace the line
document_creator_class = ShoppingProcessDocumentCreatorFetcher.new(shopping_process).call
with
document_creator_class = ShoppingProcessDocumentCreatorFetcher
and then try.
I hope this resolves your problem.
class Api::SurveyAnswersController < ApplicationController
def create
# #survey_answer = SurveyAnswer.new(survey_answer_params)
survey_answers = []
survey_id = params[:survey_id]
params[:questions].each do |q|
answer = {survey_id: survey_id, option_ids: [], question_id: q[:id],
title: q[:answer]}
if q[:options].present?
selected_options = q[:answer].split(',')
selected_options.each do |selected_option|
q[:options].each do |option|
if option[:title]== selected_option
answer[:option_ids] << option[:id]
#<todo add break when in this condition
end
end
end
survey_answers << answer
end
end
puts survey_answers
# #survey_answers = SurveyAnswer.create(survey_answers)
if SurveyAnswer.create(survey_answers)
render json: survey_answers
end
end
end
I have a survey model which has some questions. Each question contains answers. When I try to hit post request through postman to insert answers, it gives 505 internal server error with message "undefined method each for nil:nilclass". Can anybody tell what the problem is?
You are trying to run the .each loop an empty object.
Make sure that both
params[:questions]
and
q[:options]
are not empty (not equal to nil).
NoMethodError sometimes sounds very unrepresentative, especially if you're just starting off with Ruby.
Try to browse Stackoverflow next time, because this has been answered here.
I'm trying to do a simple downcase and remove duplicates in rails. Also save to the database.
Tag.all.each do |tag|
tag_name = tag.name.downcase!
tag_name.uniq!
tag.save!
end
Error
NoMethodError: undefined method `uniq!' for nil:NilClass
Also tried with pluck
tag_name = Tag.pluck(:name)
tag_name.each do |tag|
name = tag.downcase!
name.uniq!
end
Error
NoMethodError: undefined method `uniq!' for nil:NilClass
Used group_by to find filter query.
found_tags = Tag.all.group_by { |tag| tag.name.downcase }
found_tags.each do |key, val|
if found_tags[key].count > 1
tag_to_keep = val.shift
val.each do |t|
t.taggings.update_all(tag_id: tag_to_keep.id)
t.destroy!
end
tag_to_keep.update(name: tag_to_keep.name.downcase)
end
end
I am running the Kaminari gem for my pagination.
Controller
def dashboard
#projects = Project.find_by_user_id(current_user)
if #projects.size > 10
#projects.page(params[:page]).per(10)
end
end
Dashboard view
= paginate #projects, :theme => 'twitter-bootstrap-3', :remote => true
In my case, the #projects is sometimes only 1 record or even zero records. When it is nil, I get an error on the params[:page] being nil.
So this works
def dashboard
#projects = Project.page(params[:page]).per(10)
end
This gets error undefined method 'page' for #<Project:0x007f8cac5f14b0>
def dashboard
#projects = Project.find_by_user_id(current_user).page(params[:page]).per(10)
end
I think it is because the #projects is only a couple of records which is less than the 10 specified in .per
I tried adding a #projects.count or #projects.size but I get the error undefined method 'size' for #<Project:0x007f8c996865f0>
def dashboard
#projects = Project.find_by_user_id(current_user)
if #projects.size > 10
#projects.page(params[:page]).per(10)
end
end
What the hell am I doing wrong!? haha
I am guessing I can fix this in the first instance instead of trying to fix the second or third options. Any help would be greatly appreciated.
The issue is Project.find_by_user_id(current_user) returns an Array, not an ActiveRecord::Relation
You should do something like:
current_user.projects.page(params[:page]).per(10)
If your relationships are correctly setup.
Or:
Project.where(user_id: current_user.id).page(params[:page]).per(10)
def creation
(1..params[:book_detail][:no_of_copies].to_i).each do |i|
logger.info "nnnnnnnnnnn#{i}"
#book_details= BookDetail.new(params[:book_detail])
#book_details.save
end
And the Error is
undefined method []' for nil:NilClass
app/controllers/book_details_controller.rb:16:increation'
Is anybody can tell what is the problem?
Error you are getting is because params[:book_detail] is nil and you are calling [:no_of_copies] on it i.e. nil.So it is giving following error
undefined method []' for nil:NilClass
So you need to check first if params[:book_detail] is present or not like following
(1..params[:book_detail][:no_of_copies].to_i).each do |i|
logger.info "nnnnnnnnnnn#{i}"
#book_details= BookDetail.new(params[:book_detail])
#book_details.save
end if params[:book_detail] && params[:book_detail][:no_of_copies]
In addition is Salil's answer, you can use fetch
params.fetch(:book_detail, {})[:no_of_copies]
which will return nil if params[:book_detail] is nil. (1..0).to_a returns an empty array so you can rewrite your code using the following
copies = (params.fetch(:book_detail, {})[:no_of_copies] || 0).to_i
(1..copies).each do |i|
logger.info "nnnnnnnnnnn#{i}"
#book_details= BookDetail.new(params[:book_detail])
#book_details.save
end