しばらくの間、データベースに「偽の」データがロードされていました。これで、データを再更新するために必要な変更を十分に行いました。データを入力した 3 つのテーブルがあります... Users、Stores、およびGears。私の問題は Gears テーブルの作成です。私は2つの問題を抱えています。まず最も重要なことは...特定の列(user_id)の後にデータベースへの入力を停止することです。タスクの残りの部分が機能するために、データベースにある列を明示的にスキップする必要がありますか? 以下の私のコードを参照してください...
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
第二に、データベースの作成中にランダムにこのエラーが発生し続けます...私はそれを理解できません:
bundle exec rake db:populate
rake aborted!
undefined method `name' for nil:NilClass
ギアモデル
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
データベース(ギアテーブル)
`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;
私の環境:
- レール 3.2.0
- Ruby ruby 1.9.3p0 (2011-10-30 リビジョン 33570) [x86_64-darwin11.2.0]
- 宝石「フェイカー」、「1.0.1」