I would like that if a user type "100,50" or "100.50" is the same thing. 100$ and 50 cents.
before_validation :strip_commas
def strip_commas
unless self.total_less_tax.blank?
self.total_less_tax = total_less_tax.tgsub(",", ".").to_f
end
end
but I get this error
NoMethodError (undefined method `gsub' for #<BigDecimal:7fc1ba371f08,'0.1E6',9(18)>):
app/models/invoice.rb:17:in `strip_commas'
app/controllers/invoices_controller.rb:35:in `update'
You can call gsub method over the string but you are calling that on Big Decimal.So try this
def strip_commas
unless self.total_less_tax.blank?
self.total_less_tax = total_less_tax.to_s.gsub(",", ".").to_f
end
end
Related
How to parameterize enum? I want to use my enum value in method any? to check true or false for my ActiveRecord
I have method is_any? to check my ActiveRecord
def is_any? status
album.voice_guides.all_except(params[:ids]).any?(&status?)
end
And call is_any? in this method
def verify_bulk_destroy
return if album.pending?
if album.publish?
raise Api::Error::ControllerRuntimeError, :error unless is_any?
(:publish)
end
end
But it raise error
undefined method `status?' for #<Api::Admin::Albums::PremiumGuidesController:0x0000000000d278>
Did you mean? status
status=
Just change your is_any? method to
def is_any?(status)
album.voice_guides.all_except(params[:ids]).any? { |guide| guide.status == status }
end
which will load all records into memory first as your method did before.
Or to this when you want to check the condition in the database without loading the records into memory which might be faster depending on your needs:
def is_any?(status)
album.voice_guides.all_except(params[:ids]).where(status: status).exists?
end
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.
Hi Together I've got this code:
#coursesFound = #user.available_courses
#courses = []
for course in #coursesFound do
#courseInGroups = course.user_groups
for group in #courseInGroups do
#group = UserGroup.find group.id
if #group.users.map { |u| u.id }.include? #user.id
#courses << course
break
end
end
end
# Wenn ein Kurs keiner Gruppe hinzugefĆ¼gt wurde
if #courseInGroups.empty?
#courses << course
end
on my debian vm it works fine but on my live system I got this error:
undefined method `empty?' for nil:NilClass
How can I avoid this?
If this #coursesFound = #user.available_courses returns an empty activerecord relation.
Then this won't execute
for course in #coursesFound do
#courseInGroups = course.user_groups
for group in #courseInGroups do
#group = UserGroup.find group.id
if #group.users.map { |u| u.id }.include? #user.id
#courses << course
break
end
end
end
Which means when you get here #courseInGroups is nil
if #courseInGroups.empty?
#courses << course
end
So your quick fix would be
if #courseInGroups && #courseInGroups.empty?
#courses << course
end
You can use the try method to Avoid this error:
#courseInGroups.try(:empty?)
This won't throw an error if #courseInGroups was nil.
And don't forget blank? when using rails. Here you find a good overview of all methods with or without rails.
I did not analyze your code, it's just for you, me and others that do not use this methods often, mix them up and then come here - just to remember: empty? is not blank?.
You need to properly initialize your object as well.
#courseInGroups = course.user_groups || []
You won't get nil:NilClass error any more if you initialize properly.
To get rid of nil:NilClass error you can use other answer. like try etc.
You can put the ? before the dot of the empty:
if #courseInGroups?.empty
This is the error:
ActiveRecord::AssociationTypeMismatch in VideosController#update
Topic(#2173382840) expected, got String(#2148246520)
It's from this method:
def assign_topics
if #topic_names
self.topics = #topic_names.each do |name|
Topic.find_or_create_by_name(name)
end
end
end
What is this error saying? I don't understand there being a type "topic" when topic is a model for me...
"each" returns the original array or enumerable, try using "map" instead
Hello I have the following in my Create Controller:
def create
#requestable = find_requestable
if !#requestable.nil?
. ..
And then:
def find_requestable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
this works find when find_requestable sends back a paratemter, but if it doesn't it's not sending back a NIL which causes everything to error with:
NameError (uninitialized constant Undefined):
app/controllers/request_threads_controller.rb:133:in `find_requestable'
app/controllers/request_threads_controller.rb:131:in `each'
app/controllers/request_threads_controller.rb:131:in `find_requestable'
app/controllers/request_threads_controller.rb:52:in `create'
app/middleware/flash_session_cookie_middleware.rb:14:in `call'
Why isn't find_requestable sending back nil? thank you
Third line of your find_requestable method, first check to see if name is blank. Give that a shot.
if !name.blank? && name =~ /(.+)_id$/
UPDATE: Answer in Comment:
The only other thing I can think of is that one of your params has Undefined as its value, which is causing return $1.classify.constantize.find(value) to throw up. Can you take a look at the HTTP Headers and the POST parameters that are being sent across the wire?