Can't convert fixnum to string during rake db:create
The YAML parser has to guess what data type each value in your database.yml
file is, without any other contextual information.
As such, it probably considers your password
value of 123
to be an integer.
Trying forcing the string interpretation:
password: "123"
Can't convert from Fixnum into String
Almost every object, going all the way up to Object
, contains a to_s
method on it.
Object.new.to_s #=> "#<Object:0x1668fa8>"
This means that your problem isn't in the to_s
method that is getting called by <%= ... %>
.
Looking at the event.rb
file in the rmeetup gem, you'll notice:
self.time = DateTime.parse(event['time'])
This means that the instance variable you are calling with result.time
is of the type DateTime
. Also note that it is getting set by using DateTime.parse
which takes a string. So what happens when you pass a Fixnum to DateTime.parse
?
DateTime.parse(42) #=> 'no implicit conversation of Fixnum into String'
According to the MeetUp API, the time
is returned as:
UTC start time of the event, in milliseconds since the epoch
The original RMeetup gem was last updated 5 years ago. Are you using a more recent fork? If not, it could be that the gem has simply broken as new APIs are released by MeetUp. I would suggest either finding a more up-to-date gem for accessing the MeetUp API or fork the gem and make your own alterations.
Can't convert Fixnum into String - Devise/sessions Controller issue
Ok so I finally found the answer. The db got corrupt. Once I dumped the entire db, deleted the db file and re-created everything - I was good to go.
sigh
I just hope I can figure out a way to prevent that from happening in production.
Getting error Can't convert FixNum into Array
The following line should return a ActiveRecord::Relation
into qty
:
qty = CartItem.select(:quantity).where(:cart_id => kart.id, :product_id => product.id)
You should use qty.count
instead: qty.count == 0
Also, you can't add a ActiveRecord::Relation
with 1
like this: qty+1
. It will give you the error message you had.
I'm not sure what you are trying to do, but I suggest you use the debugger
gem to help you troubleshoot your problem. Follow the guide here to setup, it's very simple to setup: http://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debugger-gem
Then, place debugger
in your code:
product = Product.find(params[:product_id])
kart = initialize_cart
qty = CartItem.select(:quantity).where(:cart_id => kart.id, :product_id => product.id)
debugger # <---- here
if qty == 0
@item = CartItem.new(:cart_id => kart.id, :product_id => product.id, :quantity => qty+1)
if @item.save
Then you can find out more while you stopped at the debugger breakpoint, you can do stuff like:
qty.class
qty.count
# etc
Also, you can run rails console
for testing.
TypeError: no implicit conversion of Fixnum into Hash - Ruby
Why is the error happening?
You are getting the error because when you run:
data[:requests] = dates_init_hash.merge(requests.count)
dates_init_hash
is a hash, and requests.count
is a number. So you are trying to merge a number into a hash, what is not allowed.
How can I fix it?
If what you want is to have the dates_init_hash
with a mapping date => number of requests that date
, you can do the following:
# initializes dates_init_hash with desired dates
dates_init_hash = {}
(start_time.to_date..end_time.to_date).each do |date|
dates_init_hash[date.iso8601] = 0
end
# iterates over reuests and add 1 to the date in dates_init_hash for each request in the desired date
requests.each do |request|
date = Date.parse(request[:request_time]).iso8601
next if dates_init_hash[date].nil?
dates_init_hash[date] += 1
end
Then, dates_init_hash
will be something like
{"2022-04-04"=>1, "2022-04-05"=>0, "2022-04-06"=>1, "2022-04-07"=>0}
TypeError (String can't be coerced into Fixnum)?
Ruby uses +
as a combination tool for strings AND mathematical situations.
Simple fix:
def rep_score(user)
rep = 0
user.badges.each do |b|
rep = rep + b.rep_bonus.to_i
end
return rep
end
to_i
is changing rep_bonus
, which is probably from the database model, from a string result into an integer. There are a few different typecasts you can set. To name a few conversions:
- Array:
to_a
- Float:
to_f
- Integer:
to_i
- String:
to_s
Mysql2 0.3.15 TypeError: can't convert nil into String
I turns out I had hardcoded my GEM_PATH
and GEM_HOME
rather than letting the gems install in the appropriate rbenv
folder. Since I had both ruby 1.9 and 2.1.1 installed, when I installed the new mysql2
gem, the C extensions were compiled against 2.1.1 and caused the above type errors when I tried to connect to the DB. Removing GEM_PATH
and GEM_HOME
and reinstalling my gems under the .rbenv
folder fixed the issue.
Related Topics
Error Installing Any Ruby Version with Rvm on Osx
Ruby Koans: Why Convert List of Symbols to Strings
Rails 5.2.0 with Ruby 2.5.1 Console - 'Warning:' 'Already' Initialized Constant Fileutils::Version
Ruby Singleton Methods with (Class_Eval, Define_Method) VS (Instance_Eval, Define_Method)
Does Sublime Text Have Support for Ruby API (Auto-Complete)
Suitability of Rails, Padrino, and Sinatra for Building a Prepaid Mobile Service
Rails 3: Generate Unique Codes (Coupons)
How to Check If a Number Is Included in a Range (In One Statement)
Is There a Good Admin Generator for Ruby on Rails
What Alternatives to Irb Are There
Profile a Rails Controller Action
How to Update Gems on Production Server
Ruby Methods That Either Yield or Return Enumerator
Rails: How to Show User's "Last Seen At" Time
Better Way to Turn a Ruby Class into a Module Than Using Refinements