rake db:seed generates ArgumentError: missing keywords: from, to - ruby-on-rails

I got a database to seed to test my code. When I run rake db:seed to seed the database, I get the error below.
I expected the console to be blank: this means the data has been seeded.
Error:
[19:19:41] (master) simple-partials-lab-noukod-000
// ♥ rake db:seed
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/core_ext/object/duplicable.rb:111: warning: BigDecimal.new is deprecated; use BigDecimal() method instead.
rake aborted!
ArgumentError: missing keywords: from, to
/usr/local/rvm/gems/ruby-2.6.1/gems/faker-2.2.1/lib/faker/default/date.rb:6:in `between'
/home/considerate-transform-1229/simple-partials-lab-noukod-000/db/seeds.rb:9:in `block in <top (required)>'
/home/considerate-transform-1229/simple-partials-lab-noukod-000/db/seeds.rb:8:in `times'
/home/considerate-transform-1229/simple-partials-lab-noukod-000/db/seeds.rb:8:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.6.1/gems/activesupport-4.2.11.1/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.6.1/gems/railties-4.2.11.1/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.6.1/gems/activerecord-4.2.11.1/lib/active_record/tasks/database_tasks.rb:253:in `load_seed'
/usr/local/rvm/gems/ruby-2.6.1/gems/activerecord-4.2.11.1/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.6.1/bin/ruby_executable_hooks:24:in `eval'
/usr/local/rvm/gems/ruby-2.6.1/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

Looks like you're using Faker with the old syntax. It recently changed to require keyword arguments rather than positional arguments.
You need to follow the updated syntax:
Faker::Date.between(from: 2.days.ago, to: Date.today) #=> "Wed, 24 Sep 2014"
Note the keywords from: and to: which specify the arguments. Order of arguments is not important, and the reverse order will work just the same:
Faker::Date.between(to: Date.today, from: 2.days.ago) #=> "Wed, 24 Sep 2014"
Additional information available in their docs: https://github.com/faker-ruby/faker/blob/master/doc/default/date.md

Related

Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller

I am trying to run a single test file: bundle exec rspec spec/models/user_spec.rb
But get following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller
NameError:
uninitialized constant Rails::Controller
Did you mean? ApiController
# ./spec/rails_helper.rb:149:in `block in <top (required)>'
# ./spec/rails_helper.rb:61:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
Initially, I was getting following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: module Shoulda::Matchers::ActiveModel
NameError:
uninitialized constant Shoulda
# ./spec/support/matchers/validate_kept_of_matcher.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:51:in `block in <top (required)>'
# ./spec/rails_helper.rb:51:in `each'
# ./spec/rails_helper.rb:51:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
But then it fixed after I added following to the test file.
require "shoulda/matchers"
I am newbie in Ruby/Rails world, can someone please give a direction?
Rails::Controller::Testing::TemplateAssertions was removed in Rails 5.
You can re-add the depreciated functionality by installing the Rails controller testing gem. However the use of controller specs, assigns and template assertions is discouraged by both the RSpec and Rails teams and is not very future proof.
The community accepted solution is to write request specs and stop poking inside your controllers.

Update crontab (Whenever gem)

By executing the whenever --update-crontab command, the following error returns:
/usr/lib/ruby/vendor_ruby/whenever/cron.rb:132:in `parse_as_string': Couldn't parse: 31557600 (ArgumentError)
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:112:in `parse_time'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:50:in `time_in_cron_syntax'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:42:in `output'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:36:in `block (2 levels) in output'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:35:in `each'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:35:in `block in output'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:34:in `each'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:34:in `output'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:151:in `block (2 levels) in cron_jobs'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:147:in `each'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:147:in `block in cron_jobs'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:146:in `each'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:146:in `cron_jobs'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:65:in `generate_cron_output'
from /usr/lib/ruby/vendor_ruby/whenever.rb:10:in `cron'
from /usr/lib/ruby/vendor_ruby/whenever/command_line.rb:54:in `whenever_cron'
from /usr/lib/ruby/vendor_ruby/whenever/command_line.rb:106:in `updated_crontab'
from /usr/lib/ruby/vendor_ruby/whenever/command_line.rb:35:in `run'
from /usr/lib/ruby/vendor_ruby/whenever/command_line.rb:6:in `execute'
from /usr/bin/whenever:41:in `<main>'
My schedule.rb file:
every 1.year do
rake "maintenances:renew"
end
Any ideas?
If you're having problems with 1.year method, you can use the two following alternatives.
1 - Shortcuts
every :year do
rake "maintenances:renew"
end
2 - Expressions
every '0 0 1 12 *' do
rake "maintenances:renew"
end
If you want to learn more about these options, check the official documentation.
To test expressions, I recommend crontab.guru

unable to run rake task with smarter_csv

I created a rake task for import a csv with smarter_csv gem.
Throws the following message, i added the entire error code if it helps:
rake import_csv:import_csv
Digest::Digest is deprecated; use Digest
I, [2017-03-01T11:46:08.551994 #7029] INFO -- : ** [Raven] Raven 0.14.0 configured not to send errors.
rake aborted!
NoMethodError: undefined method `close' for nil:NilClass
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:223:in `ensure in process'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:223:in `process'
/home/my_user/my_app/lib/tasks/import_csv.rake:7:in `block (2 levels) in <top (required)>'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `eval'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `<main>'
Errno::ENOENT: No such file or directory # rb_sysopen - /csv_imports/pacientes.csv
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `initialize'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `open'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/gems/smarter_csv-1.1.4/lib/smarter_csv/smarter_csv.rb:23:in `process'
/home/my_user/my_app/lib/tasks/import_csv.rake:7:in `block (2 levels) in <top (required)>'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `eval'
/home/my_user/.rvm/gems/ruby-2.1.1#my_app/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => import_csv:import_csv
(See full trace by running task with --trace)
This is the code on the rake task:
namespace :import_csv do
desc "import csv file"
task import_csv: :environment do
require 'smarter_csv'
filename = '/csv_imports/file.csv'
options = {:key_mapping => { :downcase_header => true }}
SmarterCSV.process(filename, options) do |array|
CsvImports.create( array.first )
end
end
end
This solved just witha dot. route issue.
./csv_imports/file.csv

RubyOnRails RequireJS + conditioner js - precompile failed

I've a huge problem with asset precompile on production mode...
I'm using RequireJS and Conditioner.js to modularize my frontend, there's no problem on my development server (without asset precompilation), but I can't precompile asset on production.
Here is my requirejs.yml config:
map:
'*':
conditioner: "lib/conditioner"
modules:
- name: "application"
- name: "jquery"
- name: "conditioner"
paths:
jquery: "lib/jquery"
conditioner: "lib/conditioner"
priority: ['jquery', 'conditioner', 'application']
Here is a stack from my console:
Requirejs::BuildError: Cannot compute digest for missing asset:/home/birdnet/rails_projects/reczna-chks.pl/tmp/requirejs/dst/conditioner.js
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/gems/requirejs-rails-0.9.5/lib/requirejs/rails/builder.rb:22:in `digest_for'
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/gems/requirejs-rails-0.9.5/lib/tasks/requirejs-rails_tasks.rake:149:in `block (5 levels) in <top (required)>'
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/gems/requirejs-rails-0.9.5/lib/tasks/requirejs-rails_tasks.rake:149:in `sub'
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/gems/requirejs-rails-0.9.5/lib/tasks/requirejs-rails_tasks.rake:149:in `block (4 levels) in <top (required)>'
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/gems/requirejs-rails-0.9.5/lib/tasks/requirejs-rails_tasks.rake:146:in `each'
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/gems/requirejs-rails-0.9.5/lib/tasks/requirejs-rails_tasks.rake:146:in `block (3 levels) in <top (required)>'
Tasks: TOP => requirejs:precompile:all => requirejs:precompile:digestify_and_compress
(See full trace by running task with --trace)
rake aborted!
Command failed with status (1): [/home/birdnet/.rvm/rubies/ruby-2.1.2/bin/r...]
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/gems/requirejs-rails-0.9.5/lib/tasks/requirejs-rails_tasks.rake:19:in `ruby_rake_task'
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/gems/requirejs-rails-0.9.5/lib/tasks/requirejs-rails_tasks.rake:86:in `block (3 levels) in <top (required)>'
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/bin/ruby_executable_hooks:15:in `eval'
/home/birdnet/.rvm/gems/ruby-2.1.2#reczna-chks/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => assets:precompile => requirejs:precompile:external
It seems to be a bad path in configuration, but in my opinion every path looks ok.
Can anyone help me? I'm really stuck but won't to disable precompilation on production.
Try changing the path in modules to:
- name: "lib/conditioner"

Adding to seeds.rb of an existing project, Validation errors and undefined methods

I've been tasked with adding seeds to an existing Rails project. I've been given a list of twelve survey names and I'm supposed to create them all, storing their id (position in the list of surveys) and name values, then loop over them and add a placeholder survey_question to each. I know how to do it if I were to create a new migration, redefining survey_question as an attribute of survey, but I'm supposed to work with the existing structure where survey and survey_question are distinct classes. And I've hit a wall.
My thinking was to put all the survey names in an array, thus ordering them automatically when the database is seeded:
surveys = [
"SurveyA",
"SurveyB",
"SurveyC",
"SurveyD",
"SurveyE",
"SurveyF",
"SurveyG",
"SurveyH",
"SurveyI",
"SurveyJ",
"SurveyK",
"SurveyL"
]
Then write an each loop that instantiates the Survey class and stores the given name as the name attribute of the database entry. Within that same loop I define survey_question as a placeholder question (which some of you may recognize), and attempt to append it to the Survey being created with the << operator. This is my first point of failure:
surveys.each do |survey|
survey_question = SurveyQuestion.create(body: "What is the average flight speed velocity of an unladen swallow?")
Survey.create(name: survey) << survey_question
end
Great, I've written a loop. I need to verify that it does what I want it to do. So I go to re-seed the database and run rake db:seed. When I do, however, I get a validation error from a line in the seeds.rb file that came with the project. See here:
rake aborted!
undefined method `<<' for #<Survey:0x007fb01fe1a3d0>
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activemodel-4.0.3/lib/active_model/attribute_methods.rb:439:in `method_missing'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/attribute_methods.rb:155:in `method_missing'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:95:in `block in <top (required)>'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:93:in `each'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:93:in `<top (required)>'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `block in load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/railties-4.0.3/lib/rails/engine.rb:540:in `load_seed'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:154:in `load_seed'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:140:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:setup => db:seed
Here is the offending line (25) from the seeds:
#create seed region
usa = Region.create!(name: "USA")
So I thought, maybe for some wierd reason this was written when the database was created and cannot be overwritten, so if I clear out the database and start anew, it will populate without complaining. I ran rake db:reset. Wrong. At this point I realized there was an error in the way I'd written my each loop above. The error message told me that << is an undefined method for the survey object it was attached to. Here's that messsage.
rake aborted!
undefined method `<<' for #<Survey:0x007fb01fe1a3d0>
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activemodel-4.0.3/lib/active_model/attribute_methods.rb:439:in `method_missing'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/attribute_methods.rb:155:in `method_missing'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:95:in `block in <top (required)>'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:93:in `each'
/Users/bwstud/Box Sync/Work/Codefellows/erp/db/seeds.rb:93:in `<top (required)>'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `block in load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/railties-4.0.3/lib/rails/engine.rb:540:in `load_seed'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:154:in `load_seed'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/Users/bwstud/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.3/lib/active_record/railties/databases.rake:140:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:setup => db:seed
As a final point of data I checked out what was actually in the database, just to see what was there. This is the result of SELECT * FROM "regions";:
erp_development=# SELECT * FROM "regions"
erp_development-# ;
id | name | created_at | updated_at | default_email_sig
----+------+----------------------------+----------------------------+-------------------
1 | USA | 2014-08-01 17:13:06.733146 | 2014-08-01 17:13:06.733146 |
(1 row)
So now I'm good and stuck. I've been learning as much as I can but my understanding of Ruby and Ruby on Rails is entry-level at best. If anyone out there in Stack-land can help shed some light on this for me I will forever be in your debt.
Thanks,
Brian
<< is Ruby Array method. Survey.create(name: survey) won't return you array, so this is the reason why it fails.
According to the documentation .create method returns the resulting object instead! http://apidock.com/rails/ActiveRecord/Base/create/class
survey = Survey.create(name: survey)
survey.survey_questions << question
The code above should work!
The << method just modifies the returning value of survey.survey_questions, it does not modify the object itself. You should use modifying methods like = or +=. Anyway, I think this is a cleaner way to do the same:
surveys.map do |survey|
Survey.create! do |s|
s.name = survey
s.survey_questions.build(body: "What is the average flight speed velocity of an unladen swallow?")
end
end

Resources