Rails numericality validation is failing with a Fixnum value - ruby-on-rails

I'm using Ruby 2.2.3 with Rails 4.2.8 and Oracle DB. I have a model called Historico, which has the following validation:
validates :anosemestre, presence: true, numericality: { only_integer: true }
Also, in my schema, I have the correct declaration for the anosemestre column:
t.integer 'anosemestre'
I'm trying to clone an old Historico to a new one by doing somethings like (historico_old is fresh from the DB):
attributes = historico_old.attributes
attributes.delete('id')
historico_novo = Historico.new(attributes)
binding.pry # for debug, obviously
(...)
historico_novo.save
But when I try to save it, the validation fails saying that anosemestre is not a number. When I debug it, I get the following output from pry, which made me even more confused:
[1] pry(Historico)> historico_novo.valid?
=> false
[2] pry(Historico)> historico_novo.errors.messages
=> {:anosemestre=>["is not a number"]}
[3] pry(Historico)> historico_novo.anosemestre
=> 20171
[4] pry(Historico)> historico_novo.anosemestre.class
=> Fixnum
[5] pry(Historico)> historico_novo.anosemestre = historico_novo.anosemestre.to_i
=> 20171
[6] pry(Historico)> historico_novo.anosemestre.class
=> Fixnum
[7] pry(Historico)> historico_novo.valid?
=> true
This issue did not occur in any other model that we have (and we have plenty), and is only happening when I copy the attributes from one historico to another. I have absoloutely no idea how to proceed from here, has anyone else experienced this issue before?

Related

The provided regular expression is using multiline anchors (^ or $), which may present a security risk

I am using Ruby 2.3.0 and Rails 4.2.5 and I am follow this document https://www.sitepoint.com/youtube-rails/
Since here there is regular expression are used to validate a youtube url. I am copy paste it in my rails application its working fine on rails console
[1] pry(main)> YT_LINK_FORMAT = /^(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?([\w-]{10,})/
=> /^(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?([\w-]{10,})/
[2] pry(main)> video_url = "https://www.youtube.com/watch?v=aZngT1Eas4w"
=> "https://www.youtube.com/watch?v=aZngT1Eas4w"
[3] pry(main)> uid = video_url.match(YT_LINK_FORMAT)
=> #<MatchData "https://www.youtube.com/watch?v=aZngT1Eas4w" 1:"aZngT1Eas4w">
[4] pry(main)> uid[2]
=> nil
[5] pry(main)> uid[1]
=> "aZngT1Eas4w"
[6] pry(main)> video_url = "https://youtu.be/aZngT1Eas4w"
=> "https://youtu.be/aZngT1Eas4w"
[7] pry(main)> uid = video_url.match(YT_LINK_FORMAT)
=> #<MatchData "https://youtu.be/aZngT1Eas4w" 1:"aZngT1Eas4w">
[8] pry(main)> uid[1]
=> "aZngT1Eas4w"
But when I run my rails application I got this error
"The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?"
I am also try to use this regular expression
/^(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?([\w-]{10,})/
=> /^(?:https?:\/\/)?(?:www\.)?youtu(?:\.be|be\.com)\/(?:watch\?v=)?([\w-]{10,})/
But the same problem is still there
This regular expression is working for rails 5
YT_LINK_FORMAT = /(http:\/\/|https:\/\/|)(www.)?(youtu(be\.com|\.be|be\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/

Gibbon::GibbonError at /visitors You must set an api_key prior to making a call

Running OSX Mavericks, ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0], rvm 1.25.23 (master), and rails-4.1.0 (allegedly)
I'm working through the railsapps.org book on learning rails I have finished implementing the mailchimp email list code, and when I press submit on my form, I get the following error:
Gibbon::GibbonError at /visitors
You must set an api_key prior to making a call
I was working through an invalid URI error, and it mysteriously disappeared over the weekend (I haven't touched the Mac since last Friday). Now I have this new error.
My API Key and List ID are valid and properly set. If I look back in the log, I see another error that the mailchimp.lists.subscribe method is missing.
Here's the code as implemented from the book:
class Visitor < ActiveRecord::Base
has_no_table
column :email, :string
validates_presence_of :email
validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
def subscribe
mailchimp = Gibbon::API.new
result = mailchimp.lists.subscribe({
:id => Rails.application.secrets.mailchimp_list_id,
:email => {:email => self.email},
:double_optin => false,
:update_existing => true,
:send_welcome => true
})
Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
end
end
I hate being a noob when I can't debug for myself... Replies are appreciated.
Regards,
Jeff
Gibbon automatically looks for the environment variable MAILCHIMP_API_KEY and Gibbon will use it when you create an instance. If you haven't set a Unix env variable MAILCHIMP_API_KEY, you will need to set it explicitly. To hardcode it:
mailchimp = Gibbon::API.new("your_api_key")
Or obtain it from the config/secrets.yml file:
mailchimp = Gibbon::API.new(Rails.application.secrets.mailchimp_api_key)

HBase via MassiveRecord in Ruby is causing an abort

When using MassiveRecord over thrift to save a record into HBase, I get a strange "abort" error.
Here is some code that will reproduce the error on Mac OS X, with hbase (0.92.0 and 0.94.0) install via homebrew.
require 'massive_record'
MassiveRecord::ORM::Base.connection_configuration = { :host => 'hbase' }
class Woot < MassiveRecord::ORM::Table
default_scope select(:data)
column_family :data do
field :name, :string
end
end
woot = Woot.new( :name => 'rawr' )
woot.save
This always causes the process to halt, leaving the message
[1] 8756 abort ruby massive_woot.rb
Retrieving works just fine, but I can't seem to save the records.
Here is what the schema looks like:
>> describe 'woots'
DESCRIPTION ENABLED
{NAME => 'woots', FAMILIES => [{NAME => 'data', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3', C true
OMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCA
CHE => 'true'}]}
1 row(s) in 0.0190 seconds
This turned out to be a versioning issue. MassiveRecord v0.2.2 depends on thrift 0.6.0 (specified by an "= 0.6.0" version in the Gemfile).
I solved this for now by pulling off the "develop" branch of MassiveRecord from github.

How to test for a time span parameter?

I'm struggling with the following issue:
$ rails c
Loading development environment (Rails 3.2.1)
irb(main):001:0> 2.class
=> Fixnum
irb(main):002:0> Fixnum === 2
=> true
irb(main):003:0> 2.hours.class
=> Fixnum
irb(main):004:0> Fixnum === 2.hours
=> false
irb(main):005:0>
I'd like to test whether some specified parameter is a symbol or a time span. The way I thought would be the natural way in Ruby/Rails is:
case param
when :today then
# do this...
when Fixnum then
# do that...
else
raise ArgumentError ...
end
As far as I can tell from ActiveSupport's source code === is not overridden for Fixnum. So, what am I doing wrong?
You can see from the console, that 2.hours can't be Fixnum, because it's #inspect method is overridden. It just pretends to be Fixnum, but it is ActiveSupport::Duration (see the source)
> 2.hours
=> 7200 seconds
> 2.hours.to_i
=> 7200
Better check if it responds to some methods. If it responds to to_i, it is not a symbol.
> :today.respond_to?(:to_i)
=> false
> 2.hours.respond_to?(:to_i)
=> true

Why is this RSpec test failing?

I'm in the process of learning Ruby on Rails, so treat me like a total neophyte, because I am.
I've got a User model with some associated RSpec tests, and the following test fails:
require 'spec_helper'
describe User do
it 'should require a password' do
User.new({:email => 'valid_email#example.com', :password => '', :password_confirmation => ''}).should_not be_valid
end
end
The relevant part of the User model looks like this:
class User < ActiveRecord::Base
...
validates :password, :presence => true,
:confirmation => true,
:length => { :minimum => 6 }
...
end
Here's the catch: if I run User.new(...).valid? from a Rails console using the arguments above, it returns false as expected and shows the correct errors (password is blank).
I was using spork/autotest and I restarted both to no avail, but this test also fails even running it directly with rspec. What am I doing wrong here?
EDIT
I tried a few more things with the test. This fails:
u = User.new({:email => 'valid_email#example.com', :password => '', :password_confirmation => ''})
u.should_not be_valid
So does this:
u = User.new({:email => 'valid_email#example.com', :password => '', :password_confirmation => ''})
u.valid?
u.errors.should_not be_empty
This passes, confirming that :password is indeed blank:
u = User.new({:email => 'valid_email#example.com', :password => '', :password_confirmation => ''})
u.password.should == ''
So, it's actually spork that is causing the problem. You can turn caching off, so that it won't need restarting every time :
http://ablogaboutcode.com/2011/05/09/spork-testing-tip-caching-classes
I think this is what happens :
ruby-1.9.2-p180 :020 > u = User.new
=> #<User id: nil, email: ...
ruby-1.9.2-p180 :021 > u.errors
=> {}
ruby-1.9.2-p180 :022 > u.save
=> false
ruby-1.9.2-p180 :023 > u.errors
=> {:email=>["can't be blank", "can't be blank"], ...}
In short, if you change new to create, it will work :) I think that this happens because the matcher be_valid checks on the model validation errors. There can be a deeper explanation, but i think that if you use create instead of new, it will work.
EDIT : I have a be_valid_verbose version that might help. Just create a 'be_valid_verbose.rb' file in your rspec/custom_matchers folder, and inside it write :
RSpec::Matchers.define :be_valid_verbose do
match do |model|
model.valid?
end
failure_message_for_should do |model|
"#{model.class} expected to be valid but had errors:n #{model.errors.full_messages.join("n ")}"
end
failure_message_for_should_not do |model|
"#{model.class} expected to have errors, but it did not"
end
description do
"be valid"
end
end
Now check against be_valid_verbose instead of be_valid. It will hopefully present you with some more information on what is happening in your case.
As I feared, the answer was stupidity. This was a spork problem. I thought I had killed the existing process and was running rspec independently, but I later found the spork process still running in a different shell, and rspec had been connecting to it all along. Restarting spork (or killing it entirely) and re-running the tests fixed the problem.
I found this particularly deceptive in that rspec continually updated the test output to reflect the fact that it was aware of my test changes, so it appeared to me that it was running against up-to-date code. Now I'm left to wonder what the real utility of spork is, since apparently I can't trust that it's actually running the right tests correctly.

Resources