1

我已经在我的数据库中加载了一段时间的“假”数据。现在,我已经做出了足够的更改来重新刷新数据。我已经填充了 3 个表... UsersStoresGears。我的问题是填充 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'
4

1 回答 1

1

好吧,我觉得很愚蠢..想通了。

  • 我忘了将列添加到 attr_accessible: 在模型中
  • 未定义的方法被抛出,因为假数据被放入数据库中,值为“0”,该特定对象不存在该数据。

希望这对某人有所帮助。

于 2012-05-16T14:16:38.637 回答