How to populate devise user - ruby-on-rails

I want to populate db with fake data, using faker and populator gems.
Using Devise generated a model User.
this is my rake file
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
[User, Article].each(&:delete_all)
password = "password"
User.populate 20 do |user|
user.name = Faker::Name.name
user.email = Faker::Internet.email
user.password = password
user.password_confirmation = password
Article.populate 5 do |article|
article.user_id = user.id
article.title = Populator.words(1..3).titleize
article.content = Populator.sentences(2..10)
article.created_at = 2.years.ago..Time.now
end
end
end
end
When i run rake db:populate raises the following
rake aborted!
undefined method `password=' for #<Populator::Record:0xa179d14>
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/record.rb:64:in `method_missing'
/home/sunloverz/RubymineProjects/socialnews/lib/tasks/sample_date.rake:10:in `block (3 levels) in <top (required)>'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:53:in `call'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:53:in `block in build_records'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:50:in `times'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:50:in `build_records'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:43:in `block in populate'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:29:in `remember_depth'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:42:in `populate'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/model_additions.rb:25:in `populate'
/home/sunloverz/RubymineProjects/socialnews/lib/tasks/sample_date.rake:6:in `block (2 levels) in <top (required)>'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
Tasks: TOP => db:populate
(See full trace by running task with --trace)
What is wrong?

Populator doesn't load the ActiveRecord instance; so any validations, callbacks, embedded methods wouldn't work. Instead it loads its own Record object, using columns attributes and pure SQL to boost performance.
On the other hand, Devise doesn't create :password column, it creates encrypted_password column and does all the logic behind the scene, check this link if you want to have insight about devise code.
Solution: We need to call password_digest to encrypt the given password, and then set the encrypted_password directly, but this method is protected and can't be called inside the rake. Instead we would use the 'new' method to trigger password_digest, so your code would be something like this:
password = "password"
User.populate 20 do |user|
user.name = Faker::Name.name
user.email = Faker::Internet.email
user.encrypted_password = User.new(:password => password).encrypted_password
# rest of your code here
end

Make sure that your Model (table in your case) actually has a column named "password".
If so, then, try to check if it has attr_accessible :password

Related

fedena : rake aborted school not selected

I was installing fedena on ubuntu installed on wsl and the database creation and migration was successful but when run the command rake fedena:plugins:install_all to install the plugins. in the middle this error pops up that school Not Selected and here is the error trace.
School Not Selected
/root/fedena/vendor/plugins/acts_as_multi_school/lib/multischool/read.rb:46:in `with_school'
/root/fedena/vendor/plugins/acts_as_multi_school/lib/multischool/read.rb:22:in `find_with_school'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/activerecord-2.3.5/lib/active_record/base.rb:1954:in `find'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/activerecord-2.3.5/lib/active_record/base.rb:1954:in `find_or_create_by_config_key'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/activerecord-2.3.5/lib/active_record/base.rb:1940:in `send'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/activerecord-2.3.5/lib/active_record/base.rb:1940:in `method_missing_without_paginate'
/root/fedena/vendor/plugins/will_paginate/lib/will_paginate/finder.rb:170:in `method_missing'
/root/fedena/app/models/configuration.rb:90:in `find_or_create_by_config_key'
/root/fedena/db/seeds.rb:26
/root/fedena/db/seeds.rb:2:in `each'
/root/fedena/db/seeds.rb:2
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in `load_without_new_constant_marking'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in `load'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:145:in `load'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rails-2.3.5/lib/tasks/databases.rake:215
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/root/fedena/lib/tasks/fedena_plugin_install.rake:31
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.8.7-p374/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/usr/local/rvm/gems/ruby-1.8.7-p374/gems/rake-0.8.7/bin/rake:31
/usr/local/rvm/gems/ruby-1.8.7-p374/bin/rake:19:in `load'
/usr/local/rvm/gems/ruby-1.8.7-p374/bin/rake:19
the code causing this error appears to be this code:
module MultiSchool
module Read
def self.extended (base)
eigen_class = class << base;self;end
["find","count","sum","exists?"].each do |method|
matches = method.match /([\w\d_]+)(\?|!|=)?$/
eigen_class.send :define_method, "#{matches[1]}_with_school#{matches[2]}" do |*args|
options = args.extract_options!
skip_multischool = options.delete :skip_multischool
args << options unless options.empty?
result = if skip_multischool || Thread.current[:skip_multischool].present?
send("#{matches[1]}_without_school#{matches[2]}",*args)
else
with_school do
send("#{matches[1]}_without_school#{matches[2]}",*args)
end
end
result
end
eigen_class.send :alias_method_chain, method, :school
end
end
def skip_multischool
Thread.current[:skip_multischool]=true
yield
ensure
Thread.current[:skip_multischool]=nil
end
private
def with_school
target_school = MultiSchool.current_school
if target_school.nil?
raise MultiSchool::Exceptions::SchoolNotSelected,"School Not Selected"
else
with_scope(:find => {:conditions => {:school_id => target_school.id}}) do
yield
end
end
end
end
end
looks the MultiSchool.current_school is returning nil which sets the variable target_school to nil so the above exception occurs of school not selected but I don't know where to from here?
Could you please help me out this issue.

ActionMailer w/ Custom Rake Task

I am trying to create two custom rake tasks in in Rails 4 app. They are supposed to read the agent_card table and based on the logic, fire off an email when the time is triggered. This is my first time creating a rake task and I am not getting an error, therefore I don't know if I am doing it right. I would assume when using the mailer that letter opener would work once the mailer is fired (should it all be working correctly), but I am not sure. I have posted all relevant code below. Thanks in advance!
.rake file
namespace :agent_cards do
desc 'Sends an email to an agent with a license expiring 2 months from today'
task license_expire_agent: :environment do
Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
AgentCard.all.each do |agent_card|
if agent_card.real_estate_license_expires_at == Date.today + 2.months
LicenseExpireMailer.license_expire_agent(#agent_card, #agent).deliver_later
end
end
end
end
namespace :agent_cards do
desc 'Sends an email to an the agent development manager when a license expires 1 week from today'
task license_expire_mgr: :environment do
Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}"
AgentCard.all.each do |agent_card|
if agent_card.real_estate_license_expires_at == Date.today + 7
LicenseExpireMailer.license_expire_mgr(#agent_card, #agent).deliver_later
end
end
end
end
license_expire_agent_mailer.rb
class LicenseExpireMailer < ActionMailer::Base
default from: "Mike <help#mike.com>"
def license_expire_agent(agent, agent_card)
#agent = agent
#agent_card = agent_card
mail to: "mike#mike.com", subject: 'Your license is about to expire!'
end
end
license_expire_mgr_mailer.rb
class LicenseExpireMailer < ActionMailer::Base
default from: "Mike <help#mike.com>"
def license_expire_mgr(agent, agent_card)
#agent = agent
#agent_card = agent_card
# Head of Agent Development
mail to: "mike#mike.com", subject: "#{#agent.name}'s License Expiring"
end
end
When I try to test the rake task I enter
rake agent_cards:license_expire_agent
or
rake agent_cards:license_expire_mgr
Error
NameError: uninitialized constant LicenseExpireMailer
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:7:in `block (3 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1#global/gems/activerecord- 4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1#global/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:5:in `block (2 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => agent_cards:license_expire_agent
Your mailer file name does not match with the mailer class name as per Rails convention.
Mailer class should be placed in this path:
app/mailers/license_expire_mailer.rb

Rails 4.1: Can't cast Hash to string

Created a simple rake task to find Bikes for Sale in the USA. It works when i want to extract heading details but when I add other parameters like location, price , etc and save it to the Posts db I get error Can't cast Hash to string
Neils-MacBook-Pro-2:bike_scraper neilpatel$ rake scraper:scrape
rake aborted!
TypeError: can't cast Hash to string
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/quoting.rb:76:in `type_cast'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in `type_cast'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in `block in exec_query'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `map'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `exec_query'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:68:in `exec_insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/relation.rb:64:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:503:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:87:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:306:in `block in _create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:82:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:306:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/timestamp.rb:57:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:483:in `create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:82:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in `create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:103:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/validations.rb:51:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in `block (2 levels) in save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:208:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in `block in save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:267:in `save'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:53:in `block (3 levels) in <top (required)>'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:38:in `each'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:38:in `block (2 levels) in <top (required)>'
Tasks: TOP => scraper:scrape
(See full trace by running task with --trace)
scraper.rake
namespace :scraper do
desc "Fetch Craigslist posts from 3Taps"
task scrape: :environment do
require 'open-uri' #ruby gem
require 'JSON'
# Set API token and URL
auth_token = "fadddddddd4a23a1cc86e0d"
polling_url = "http://polling.3taps.com/poll"
# Specify request parameters
params = {
auth_token: auth_token,
anchor: 2109915749,
source: "CRAIG",
category_group: "SSSS",
category: "SBIK",
'location.country' => "USA",
retvals: "location,external_url,heading,body,timestamp,price,images,annotations"
}
# Prepare API request
uri = URI.parse(polling_url)
uri.query = URI.encode_www_form(params)
# Submit request -hitting enter
result = JSON.parse(open(uri).read)
# Display results to screen (put string - display information)
#puts result["postings"].second["location"]
#store results in database
# Create new Post
result["postings"].each do |posting|
# Create new Post
#post = Post.new
#post.heading = posting["heading"]
#post.body = posting["body"]
#post.price = posting["price"]
#post.state = posting["location"]
#post.external_url = posting["external_url"]
#post.timestamp = posting["timestamp"]
# Save Post
#post.save
end
end
desc "TODO"
task destroy_all_posts: :environment do
end
end
Posts.db
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :heading
t.text :body
t.decimal :price
t.string :state
t.string :external_url
t.string :timestamp
t.timestamps
end
end
end
The error is coming from your #post.save line, so one of those fields that came back in posting is a Hash, not a String as you expect. It won't be price, so just check all the others by putsing them.
Update
I just ran your code, it's posting["location"] that's a Hash
Further Update
Looks like you just want the state out of that hash, ie.
#post.state = posting["location"]["state"]

Rails : gem "dynamoid" list_tables error

I'm not new to DynamoDB, but I'm new to DynamoID gem.
I can't achieve to make it work since I get the following error :
/aws-sdk-1.28.1/lib/aws/core/client.rb:366:in `return_or_raise': The action or operation requested is invalid. Verify that the action is typed correctly. (AWS::DynamoDB::Errors::InvalidAction)
/aws-sdk-1.28.1/lib/aws/core/client.rb:467:in `client_request'
(eval):3:in `list_tables'
/aws-sdk-1.28.1/lib/aws/dynamo_db/table_collection.rb:121:in `_each_item'
/aws-sdk-1.28.1/lib/aws/core/collection/with_limit_and_next_token.rb:54:in `_each_batch'
/aws-sdk-1.28.1/lib/aws/core/collection.rb:80:in `each_batch'
/aws-sdk-1.28.1/lib/aws/core/collection.rb:47:in `each'
/dynamoid-0.7.1/lib/dynamoid/adapter/aws_sdk.rb:185:in `collect'
/dynamoid-0.7.1/lib/dynamoid/adapter/aws_sdk.rb:185:in `list_tables'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:146:in `block (3 levels) in <module:Adapter>'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:39:in `benchmark'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:146:in `block (2 levels) in <module:Adapter>'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:25:in `block in reconnect!'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:39:in `benchmark'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:25:in `reconnect!'
/dynamoid-0.7.1/lib/dynamoid.rb:35:in `configure'
app/config/initializers/dynamoid.rb:1:in `<top (required)>'
It seems that list_tables doesn't exist, but it's a method of the AWS Ruby SDK
My model is very simple :
class Interaction
include Dynamoid::Document
table :name => :interactions, :key => :from, :read_capacity => 1, :write_capacity => 1
end
Could anybody help?
Cheers,
Emmanuel

Rake aborted Uploading images using faker for ruby project

I am following a Ruby on Rails guide and ran into a problem generating fake content using the gem "Faker". I installed faker and followed the instructions to populate users and photos into my project. I created this file in lib/tasks/populate.rake
lib/tasks/populate.rake
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
10.times do |n|
puts "[DEBUG] creating user #{n+1} of 10"
name = Faker::Name.name
email = "user-#{n+1}#example.com"
password = "password"
User.create!( name: name,
email: email,
password: password,
password_confirmation: password)
end
User.all.each do |user|
puts "[DEBUG] uploading images for user #{user.id} of #{User.last.id}"
10.times do |n|
image = File.open(Dir.glob(File.join(Rails.root, 'sampleimages', '*')).sample)
description = %w(cool awesome crazy wow adorbs incredible).sample
user.pins.create!(image: image, description: description)
end
end
end
end
Now all I am supposed to do is put "rake db:populate" in my terminal (and in the right folder). When I do this I get:
rake db:populate
[DEBUG] creating user 1 of 10
[DEBUG] creating user 2 of 10
[DEBUG] creating user 3 of 10
[DEBUG] creating user 4 of 10
[DEBUG] creating user 5 of 10
[DEBUG] creating user 6 of 10
[DEBUG] creating user 7 of 10
[DEBUG] creating user 8 of 10
[DEBUG] creating user 9 of 10
[DEBUG] creating user 10 of 10
[DEBUG] uploading images for user 15 of 25
rake aborted!
can't convert nil into String
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `initialize'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `open'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:18:in `block (4 levels) in <top (required)>'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:17:in `times'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:17:in `block (3 levels) in <top (required)>'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:15:in `each'
/Users/ScottDAlessandro/code/omrails/lib/tasks/populate.rake:15:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
Tasks: TOP => db:populate
(See full trace by running task with --trace)
My big concern here is
"rake aborted!
can't convert nil into String"
It appears it created users but did not upload any pictures.
Question:
a) Why is rake being aborted?
b) How can I delete the users I created? The only way I know is" User.all" then User.delete(put a number here)...Are there more efficient ways of doing this?
Help would be greatly appreciated :) Thanks!
Your <rails_root>/sampleimages directory is empty. Dir.glob(...) is returning an empty array and when you call sample it's returning nil. When you call File.open(nil) you get the exception and rake aborts.
If you want to delete all rows in the users table you can do User.destroy_all.

Resources