Reputation: 1203
I've had "Fake" data loaded in my database for awhile. Now, I've made enough changes I need to re-fresh the data. I have 3 tables that I've populated...Users, Stores, & Gears. My issue is populating the Gears table. I'm having 2 issues. First and most important...It stops populating the database after a certain column (user_id). Do you have to explicitly skip columns that are in the database for the rest of the task to work, or what else am I? See my code below...
sample_data.rake
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
require 'faker'
make_gear
end
# def make_users
# 100.times do |n|
# firstname = Faker::Name.first_name
# lastname = Faker::Name.last_name
# email = Faker::Name.first_name + "#{n+1}@equiptme.com"
# password = "password"
# User.create!(first_name: firstname,
# last_name: lastname,
# email: email,
# password: password,
# password_confirmation: password,
# admin: "0",
# owner: "0",
# rentor: "1")
# end
# end
# def make_stores
# users = User.all
# users.each { |user| user.create_store(storename: 'Da Hut') }
# end
def make_gear
users = User.all
50.times do |h|
users.each { |user| user.gears.create(:title => Faker::Company.catch_phrase,
:size => "Large",
:price => rand(5*100),
:sub_category_id => rand(1*61),
:year => rand(1982..2012),
:latefee => rand(1*200),
:cancellation => Faker::Lorem.paragraph(sentence_count = 3),
:minrental => Faker::Lorem.paragraph(sentence_count = 1),
:policy => Faker::Lorem.paragraph(sentence_count = 2),
:about => Faker::Lorem.paragraph(sentence_count = 2),
:address => Faker::Address.street_address(include_secondary = false),
:city => Faker::Address.city,
:state => Faker::Address.state_abbr,
:zip => Faker::Address.zip_code) }
end
end
end
Secondly, I keep getting this error at random times while populating the database...I can't figure it out:
bundle exec rake db:populate
rake aborted!
undefined method `name' for nil:NilClass
Gear Model
class Gear < ActiveRecord::Base
attr_accessible :title, :size, :price, :sub_category_id, :user_id, :image, :image_a, :remote_image_url
belongs_to :user
belongs_to :sub_category
has_one :category, :through => :sub_category
has_many :comments, :dependent => :destroy
require 'carrierwave/orm/activerecord'
mount_uploader :image, GearpicUploader
mount_uploader :image_a, GearpicUploader
validates :title, presence: true
validates :size, presence: true
validates :price, presence: true
validates :sub_category_id, presence: true
validates :user_id, presence: true
searchable do
text :title, :size, :price
text :user_name do
user.name
end
string :sub_category_name do
sub_category.name
end
string :category_name do
category.name
end
end
end
Database (Gear Table)
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`size` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`price` int(11) DEFAULT NULL,
`sub_category_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`image` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`category_id` int(11) DEFAULT NULL,
`remote_image_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`image_a` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`color` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`year` int(11) DEFAULT NULL,
`latefee` int(11) DEFAULT NULL,
`cancellation` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`minrental` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`policy` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`about` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_gears_on_user_id_and_created_at` (`created_at`,`id`) USING BTREE,
KEY `index_gears_on_user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=161 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
My environment:
Upvotes: 1
Views: 1180
Reputation: 1203
Ok I feel stupid..figured it out.
Hope this helps someon.
Upvotes: 1