I am a beginner programmer in Ruby and Ruby on Rails , I'm trying to run a rake command in my task , but when I do the following happens:
rake daily_tasks:process_api
rake aborted!
ActiveRecord::AssociationTypeMismatch: Estado(#47392639701120) expected, got NilClass(#47392580444120)
/home/thiagoamaralr/Desktop/proponente-master-4f8a3b2ddb02a90b2c173cf31383505018d02dd/app/services/create_programa_api.rb:21:in `call'
/home/thiagoamaralr/Desktop/proponente-master-74f8a3b2ddb02a90b2c173cf31383505018d02dd/lib/tasks/daily_tasks.rake:7:in `block (3 levels) in <top (required)>'
/home/thiagoamaralr/Desktop/proponente-master-74f8a3b2ddb02a90b2c173cf31383505018d02dd/lib/tasks/daily_tasks.rake:5:in `each'
/home/thiagoamaralr/Desktop/proponente-master-74f8a3b2ddb02a90b2c173cf31383505018d02dd/lib/tasks/daily_tasks.rake:5:in `block (2 levels) in <top (required)>'
Tasks: TOP => daily_tasks:process_api
(See full trace by running task with --trace)
Follow the task I'm trying to run:
namespace :daily_tasks do
desc "Process the day to day tasks"
task process_api: :environment do
SiconvApi::Programa.find.each do |programa|
if programa.data_inicio_recebimento_propostas && (programa.data_inicio_recebimento_propostas.to_date >= Date.parse("2015/06/01"))
CreateProgramaApi.call(SiconvApi::Serializers::Programa.new(programa))
end
end
end
end
And this is content create_programa_api.rb:
class CreateProgramaApi
def self.call(programa_api)
params = programa_api.to_h
params[:orgao] = Orgao.where("lower(name) = ?", programa_api[:orgao][:nome].mb_chars.downcase).first_or_create(name: programa_api[:orgao][:nome])
params[:orgao_vinculado] = Orgao.where("lower(name) = ?", programa_api[:orgao_vinculado][:nome].mb_chars.downcase).first_or_create(name: programa_api[:orgao_vinculado][:nome])
params[:orgao_executor] = Orgao.where("lower(name) = ?", programa_api[:orgao_executor][:nome].mb_chars.downcase).first_or_create(name: programa_api[:orgao_executor][:nome])
params[:estados] = []
if programa_api[:estados].size == 27
params[:estados] << Estado.find_by(sigla: 'Todos')
else
programa_api[:estados].each do |e|
params[:estados] << Estado.find_by(sigla: e)
end
end
params[:atendes] = [Atende.where("lower(name) = ?", programa_api[:atende_a].mb_chars.downcase).first_or_create(name: programa_api[:atende_a])] if programa_api[:atende_a]
params.delete(:atende_a)
programa = Programa.find_by(codigo: programa_api[:codigo])
if programa
programa.update(params)
else
Programa.create! params
end
end
end
Thanks for your attention!
You have nil object in params[:estados], and Rails can't save this association.
Easiest way to remove them is to call params[:estados].compact! after line 14
This block of code is your problem:
params[:estados] = []
if programa_api[:estados].size == 27
params[:estados] << Estado.find_by(sigla: 'Todos')
else
programa_api[:estados].each do |e|
params[:estados] << Estado.find_by(sigla: e)
end
end
If no record is found, the #find_by returns nil. This is why you are getting the error:
ActiveRecord::AssociationTypeMismatch: Estado(#47392639701120) expected, got NilClass
When calling Programa.create!(params).
One solution would be to call params[:estados].compact! after the if statement (this is using Array#compact! to remove any nil values).
Or, you could instead write that section of code like this:
params[:estados] = Estadio.where(
sigla: (programa_api[:estados].size == 27 ? 'Todos' : programa_api[:estados])
)
With this code, there is no longer a need to call compact! since we already end up with an empty array if no records are found (i.e. there are no nil values).
Note that the behaviour here isn't quite the same - what happens if there are multiple Estado records with the sigla equal to one of programa_api[:estados] or 'Todos'? Previously you were only fetching the "first" such record, whereas now you'd be fetching all of them. This may not be an issue (or may even be the correct behaviour!!) - it's just something to be aware of, at least.
Related
When I run the test, I write the following error in the console:
undefined method `split' for nil:NilClass (NoMethodError)
/Users/denis/.rvm/gems/ruby-2.1.1/gems/run_loop 1.2.6/lib/run_loop/sim_control.rb:866:in `block in sim_details'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/run_loop-1.2.6/lib/run_loop/sim_control.rb:863:in `each'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/run_loop-1.2.6/lib/run_loop/sim_control.rb:863:in `sim_details'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/run_loop-1.2.6/lib/run_loop/sim_control.rb:290:in `enable_accessibility_on_sims'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/run_loop-1.2.6/lib/run_loop/core.rb:214:in `run_with_options'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/run_loop-1.2.6/lib/run_loop/core.rb:792:in `run'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/calabash-cucumber-0.12.2/lib/calabash-cucumber/launcher.rb:755:in `block in new_run_loop'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/calabash-cucumber-0.12.2/lib/calabash-cucumber/launcher.rb:753:in `times'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/calabash-cucumber-0.12.2/lib/calabash-cucumber/launcher.rb:753:in `new_run_loop'
/Users/denis/.rvm/gems/ruby-2.1.1/gems/calabash-cucumber-0.12.2/lib/calabash-cucumber/launcher.rb:624:in `relaunch'
/Users/denis/Documents/calabash-test-ios/x-platform-example/features/ios/support/01_launch.rb:27:in `Before'
The test itself is an example from here. Here is the code for one of the errors:
def sim_details(primary_key)
unless xcode_version_gte_6?
raise RuntimeError, 'this method is only available on Xcode >= 6'
end
allowed = [:udid, :launch_name]
unless allowed.include? primary_key
raise ArgumentError, "expected '#{primary_key}' to be one of '#{allowed}'"
end
hash = {}
xctools.instruments(:sims).each do |elm|
launch_name = elm[/\A.+\((\d\.\d(\.\d)? Simulator\))/, 0]
udid = elm[XCODE_6_SIM_UDID_REGEX,0]
sdk_version = elm[/(\d\.\d(\.\d)? Simulator)/, 0].split(' ').first
value =
{
:launch_name => launch_name,
:udid => udid,
:sdk_version => RunLoop::Version.new(sdk_version)
}
if primary_key == :udid
key = udid
else
key = launch_name
end
hash[key] = value
end
hash
end
Run this command: bundle exec cucumber -p ios DEVICE_TARGET="6D45E1...6513"
Can anyone come across?
Does the DEVICE_TARGET UUID actually exist in the list of available simulators?
instruments -s will show you the devices. Switching Xcode versions and not rebooting or restarting the CoreSimulator service can result in the simulator not being found.
Looks like this code sdk_version = elm[/(\d\.\d(\.\d)? Simulator)/, 0] is not returning anything. It seems that you are expecting it to return a string. I would debug in that area.
The error is telling you that your selection is resulting in a nil object which of course has no class, and also as such has no .split method. As a result trying to invoke the .split method is giving you the error that the thing you have there, does not have a .split method defined.
I use the capybara-webkit gem to scrape data from certain pages in my Rails application. I've noticed, what seems to be "random" / "sporadic", that the application will crash with the following error:
Capybara::Webkit::ConnectionError: /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/bin/webkit_server failed to start.
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/server.rb:56:in `parse_port'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/server.rb:42:in `discover_port'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/server.rb:26:in `start'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/connection.rb:67:in `start_server'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/connection.rb:17:in `initialize'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/driver.rb:16:in `new'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/driver.rb:16:in `initialize'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit.rb:15:in `new'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit.rb:15:in `block in <top (required)>'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/session.rb:85:in `driver'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/session.rb:233:in `visit'
It happens even after it's already connected and accessed a website multiple times before. Here's a code snippet of what I'm using currently...
if site.url.present?
begin
# Visit the URL
session = Capybara::Session.new(:webkit)
session.visit(site.url) # here is where the error occurs...
document = Nokogiri::HTML.parse(session.body)
# Load configuration options for Development Group
roster_table_selector = site.development_group.table_selector
header_row_selector = site.development_group.table_header_selector
row_selector = site.development_group.table_row_selector
row_offset = site.development_group.table_row_selector_offset
header_format_type = site.config_header_format_type
# Get the Table and Header Row for processing
roster_table = document.css(roster_table_selector)
header_row = roster_table.css(header_row_selector)
header_hash = retrieve_headers(header_row, header_format_type)
my_object = process_rows(roster_table, header_hash, site, row_selector, row_offset)
rescue ::Capybara::Webkit::ConnectionError => e
raise e
rescue OpenURI::HTTPError => e
if e.message == '404 Not Found'
raise "404 Page not found..."
else
raise e
end
end
end
I've even thought perhaps I don't find out why it's happening necessarily - but just recover when it does. So I was going to do a "retry" in the rescue block for the error but it appears the server is just down - so I get the same result when retrying. Perhaps someone knows of a way I can check if the server is down and restart it then perform a retry? Thanks for the help!
So after further investigating it appears that I was generating a new Capybara::Session for each iteration of my loop. I moved it outside of the loop and also added Capybara.reset_sessions! at the end of my loop. Not sure if that helps with anything -- but the issue seems to have been resolved. I'll monitor it for the next hour or so. Below is an example of my ActiveJob code now...
class ScrapeJob < ActiveJob::Base
queue_as :default
include Capybara::DSL
def perform(*args)
session = Capybara::Session.new(:webkit)
Site.where(config_enabled: 1).order(:code).each do |site|
process_roster(site, session)
Capybara.reset_sessions!
end
end
def process_roster(site, session)
if site.roster_url.present?
begin
# Visit the Roster URL
session.visit(site.roster_url)
document = Nokogiri::HTML.parse(session.body)
# processing code...
# pass the session that was created as the final parameter..
my_object = process_rows( ..., session)
rescue ::Capybara::Webkit::ConnectionError => e
raise e
rescue OpenURI::HTTPError => e
if e.message == '404 Not Found'
raise "404 Page not found..."
else
raise e
end
end
end
end
end
I am trying to set up a custom rake task for a Rails app that checks to see if your location is within a certain area with street cleaning restrictions. If it is, you can click a button "Remind Me" that will save the user id and restriction id to a reminder table.
I have the Rufus gem running the rake task every 15 minutes.
What I then want is the rake task to check the current time and day with the day and start time that corresponds with the restriction id, and if it is within 30 minutes, it should email the user to move their car.
When I run my current code below, I get this error:
rake aborted!
undefined method `reminders' for # <ActiveRecord::Relation::ActiveRecord_Relation_Day:0x007fb38c3274f8>
/Users/m/WDI/park_pointer/lib/tasks/reminder.rake:11:in `block (2 levels) in <top (required)>'
Tasks: TOP => reminder:mail_users
Here's my entire project: https://github.com/mnicole/park_pointer
But the code for the rake task is:
namespace :reminder do
desc "REMINDER"
task :mail_users => :environment do
time = Time.new
t = Time.now
today = Day.where(:id => time.wday + 1) #returns 0 for sunday, etc
#reminders = today.reminders
#reminders.each do |reminder|
#reminder = reminder
reminder_time_in_mins = restriction.start_time.hour * 60
current_time_in_mins = (Time.now.hour * 60) + Time.now.min
# last_time_in_mins = record.last.hour * 60 + record.last.min
if current_time_in_mins > (reminder_time_in_mins - 30)
#user = User.find(reminder.user_id.last)
UserMailer.reminder_email(user_id).deliver!
end
end
end
end
I THINK I'm almost there, but I've been working on this for a few months and am kind of at a loss about the next step.
Thanks in advance!!
-Michele
The problem is with this line:
today = Day.where(:id => time.wday + 1)
This isn't returning records, it's returning an ActiveRecord::Relation, which just means that the query hasn't been evaluated yet. (It does this so you can chain together filters and it only gets evaluated to SQL when you iterate over it.
Without knowing what your Day model looks like, I think what you want is this:
today = Day.where(:id => time.wday + 1).first
This will return the first result, or nil if there were no results. Then if today is non nil, it will have the reminders method.
Can you call a class method from a rakefile (scheduler.rake)?
I am using the Heroku Scheduler add-on and wrote a task that calls a class method but I receive an error when I run 'heroku run rake auto_start_games'
Connecting to database specified by DATABASE_URL
rake aborted!
compared with non class/module
Here is my task code:
task :auto_start_games => :environment do
all_Active_Games = Game.where(:game_active => 1)
not_flag = all_Active_Games > 0
started_games = []
unless all_Active_Games.length == 0
all_Active_Games.each do |x|
if x.players >= 2
x.winning_structure = 1 if x.players < 3
Comments.gameStartComment(x.id)
Game.gameHasStartedPush(x)
x.game_initialized = 1
x.was_recently_initiated = 1
started_games << x.id
else
Game.addDaytoStartandEnd(x.id)
Comment.gamePostponedComment(x.id)
end
end
end
puts "started games #{started_games}"
end
When you invoke Rake, you can pass the --trace flag to it. This should give you a backtrace, which I suspect is going to tell you the error is on the line not_flag = all_Active_Games > 0, because all_Active_Games is an ActiveRecord relation, but you're trying to compare it to the integer 0. Bascially, you have a type error. In a static language, this wouldn't even compile.
It would also be good to also fix your indentation, choose more descriptive variable names (x -> game)
I am migrating data from a database and getting an error I cannot understand. I am new to Ruby and am looking for both what is wrong with my code and also the most effective commands for debugging. I cannot even really read my error.
Here is my error:
/Users/skline/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/activemodel-3.0.6/lib/active_model/attribute_methods.rb:367:in `method_missing': undefined method `answer=' for #<Question:0x00000102d59758> (NoMethodError)
from /Users/skline/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/activerecord-3.0.6/lib/active_record/attribute_methods.rb:46:in `method_missing'
from ./script/migrate.rb:139:in `block (2 levels) in <main>'
from /Users/skline/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/activerecord-3.0.6/lib/active_record/relation.rb:13:in `each'
from /Users/skline/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/activerecord-3.0.6/lib/active_record/relation.rb:13:in `each'
from ./script/migrate.rb:137:in `block in <main>'
from ./script/migrate.rb:111:in `each'
from ./script/migrate.rb:111:in `<main>'
Any tips for reading this error and for how to debug.
Note here is my code:
NetworkCommunications.all.each do |nc|
if nc.NETWORK_COMM_TYPE_ID==1 && nc.SENDER_CONSUMER_ID != 0
q = Question.new
q.created_at = nc.LAST_MOD_TIME
category = CommunicationInterestMapping.where(:COMMUNICATION_ID => nc.COMMUNICATIONS_ID).first
if category
cie = ConsumerInterestExpertLookup.find(category.CONSUMER_INTEREST_EXPERT_ID)
if cie
q.category = Category.find_by_name cie.CONSUMER_INTEREST_EXPERT_NAME
else
puts "No category"
end
end
message = NetworkCommunicationsMessage.where(:COMMUNICATIONS_ID => nc.COMMUNICATIONS_ID).first
q.title = message.SUBJECT
q.description = message.MESSAGE
q.permalink = message.QUESTION_SLUG
email = find_email_from_consumer_id(nc.SENDER_CONSUMER_ID)
q.user = User.find_by_email email
children = NetworkCommunications.where(:PARENT_COMMUNICATIONS_ID => nc.COMMUNICATIONS_ID)
puts children
if children
children.each do |ncc|
if ncc.NETWORK_COMM_TYPE_ID == 2
q.answer = Answer.new
q.answer.created_at = ncc.LAST_MOD_TIME
message_a = NetworkCommunicationsMessage.where(:COMMUNICATIONS_ID => ncc.COMMUNICATIONS_ID).first
q.answer.text = message_a.MESSAGE
email_a = find_email_from_consumer_id(ncc.SENDER_CONSUMER_ID)
q.answer.user = User.find_by_email email_a
end
end
end
begin
q.save!
rescue Exception => e
puts "Exception: #{e} title: #{message.SUBJECT}"
end
end
end
To read the stack dump, look at the first line then read downwards:
/Users/skline/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/activemodel-3.0.6/lib/active_model/attribute_methods.rb:367:in `method_missing': undefined method `answer=' for #<Question:0x00000102d59758> (NoMethodError)
from /Users/skline/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/activerecord-3.0.6/lib/active_record/attribute_methods.rb:46:in `method_missing'
from ./script/migrate.rb:139:in `block (2 levels) in <main>'
The first line tells you where the problem was triggered and why: In ActiveModel's attribute_methods method because no setter for answer was found in the object. This was triggered from a call in line 139 of your migrate.rb script. The trick with a stack trace is to read through it, looking for the scripts you've written. Odds are really good the problem is in our code so it's always good to start from the assumption the bug is ours.
if ncc.NETWORK_COMM_TYPE_ID == 2
q.answer = Answer.new
is where the problem is. Your Question class doesn't have a setter for answer. Either you're missing or misspelled an attribute_accessor call or misspelled a def answer= method.
To debug I recommend using Ruby Debugger 1.9. gem install ruby-debug19. It's 1.9.2 studly and easy to use. You can set a breakpoint in your code, then run it from the command-line, which will run until the breakpoint is reached and will stop in the debugger. From there you can list the current lines using l, display the contents of variables using p or do a require 'pp' if you have pretty-printer installed. You can single-step into methods using s or step over them using n, for "next". There's also c to continue, c 100 to continue to a particular line number; 100 in that example. You can use b 100 to set a break-point at line 100, and then c to run, stopping at 100 every time. irb will drop you into IRB with the variables to that point already initialized so you can poke at them. There are lots of other commands, but those are the ones I use most often.
It probably means you have defined the answer attribute for your question class:
class Question < ActiveRecord::Base
attr_accessor :answer
[...]
end
You should also learn how to use rdebug so that you can step through the code and figure this out without help.
I think your model Question doesn't have answer attribute.
In this cast you can study how to debug rails app