Undefined method `between' for Fixnum [closed] - ruby-on-rails

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a URL:
xxxx/xxx?status=2
I got the value of the :status parameter from the URL, converted it to integer, and tried to use between method on it:
params[:status].to_i.between.(0, 3)
but I get the error as below:
undefined method `between' for 1:Fixnum
I checked the class ancestors:
params[:status].to_i.class.ancestors
# => [Fixnum, JSON::Ext::Generator::GeneratorMethods::Fixnum, Integer, Numeric, Comparable, Object, ActiveSupport::Dependencies::Loadable, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
I can see Comparable, so I don't get why I couldn't do it.

You have mistake in method name: not between but between?
params[:status].to_i.between?(0, 3)
Check Ruby Doc.

Related

How to use or in checking presence? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i need to check presence of a value in an array. It can be two values i need to check with an or case if it includes in the array. I think it will be confusing from my description you will understand after you see the code below.
if user.sheet_ids.include? params[:id].to_i || params[:sheet_id].to_i
can :show, Project
end
It returns false even if sheet_id is present in the array of sheet_ids.
user.sheet_ids is an array and i need to check if any of the value includes in this array. The method i used is not seems to be working.
Is there any simple way of doing this. Thanks.
I would do something like this:
(user.sheet_ids & params.values_at(:id, :sheet_id).map(&:to_i)).any?
user.sheet_ids.include? params[:id].to_i || params[:sheet_id] is executed in this way user.sheet_ids.include?(params[:id].to_i || params[:sheet_id]). The OR statement is executed and the result is what is passed as an argument to user.sheet_ids.include?
To check if either params[:id].to_i or params[:sheet_id] is included in the array users.sheet_ids, you need to do this;
if user.sheet_ids.include?(params[:id].to_i) || user.sheet_ids.include?(params[:sheet_id].to_i)
can :show, Project
end

Resolve Fixnum error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a parameter (params[:authorization]) that comes from the URL, as you can see:
authorization_selected = params[:authorization]
new_parcel = params[:new_parcel].to_i
puts authorization_selected.class (in the console show type String)
puts new_parcel.class (in the console show type Fixnum)
In my controller, have:
#portability = Portability.new
#portability.employee_id = authorization_selected.employee_id
However this returns an error:
undefined method `employee_id' for 3:Fixnum
I need that both was integer. How do it?
You are calling the employee_id method on authorization_selectedwhich is a String and does not provide this method.
Obviously this does not work. You probably want to do
#portability = Portability.new
#portability.employee_id = authorization_selected
assuming that params[:employee] contains the employee_id and Portability is an ActiveModel or an ActiveRecord.
Perhaps you can change your form that the value can be assigned through the initializer?

Why isn't this route working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the following route to display the cars of a user:
get 'cars/id:/all' => 'wikipages#index', as: 'cars_index'
This translates to:
cars_index_path GET /cars/id:/all(.:format) cars#index
However, when I visit site-url/cars/1/all I get the error No route matches. Am I visiting the route/path incorrectly?
P.S. Controller method:
def index
#user = User.find(params[:id])
#cars = #user.garage.cars.all
end
Variable segments use a leading :, not a trailing :.
You need :id, not id:.

undefined method `days_in_a_month' for Time:Class [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I tried several things and I can't seem to find the reason why Time::days_in_a_month is not found.
I'm trying to calculate the amount of signups for each day of the year, this is the code:
require 'date'
require 'time'
class GraphicsController < ApplicationController
def year_graph(year=2013)
data=calculate_signups(year)
gon.data=data
end
def calculate_signups(year)
#binding.pry
month_days=(1..12).map{|x| Time.days_in_a_month(x,year)}
total_yeardays=month_days.sum
year_sum=Array.new
day_sum={day:0,yearday:0,num:0}
afee=User.clients.year(year)#.month(month)
month_days.each_with_index do |mindex,mdays|
(1..mdays).each do |d|
day_fees=afee..month(mindex).day(d)
dia=day_sum.dup
suma=day_fees.length
dia[:day]=d
dia[:num]=suma
dia[:yearday]=Date.new(year,mindex,d).yday
year_sum << dia
end
end
year_sum
end
end
If I try it in the debugger pry(see binding.pry line) there is no problem. And of course no problem either when I try it on the console rails c --sandbox. I assume I'm missing something, but I don't know what!
The correct syntax is without a. The correct is
Time.days_in_month

How to get a ruby class when it's name is in a variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
So I'm passing a string with the name of an activerecord model class into my method and I want to be able to call some methods on that class.
This is wrong:
def perform(body_class, body_id)
body = body_class.constantize
request = body.find(body_id)
end
Update - that actually works fine.
What error are you getting? Make sure the string is in the correct ActiveRecord format.
For example:
"Person".constantize # works
"person".constantize # does not work
To get the second one to work you should also add camelize
"person".camelize.constantize # works

Resources