【问题标题】:Creating Sample Data in MySQL Rails Database with Faker使用 Faker 在 MySQL Rails 数据库中创建示例数据
【发布时间】:2012-05-23 03:28:36
【问题描述】:

我已经在我的数据库中加载了一段时间的“假”数据。现在,我已经做出了足够的更改来重新刷新数据。我已经填充了 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;

我的环境:

  • Rails 3.2.0
  • Ruby ruby​​ 1.9.3p0(2011-10-30 修订版 33570)[x86_64-darwin11.2.0]
  • gem 'faker', '1.0.1'

【问题讨论】:

  • 不是答案(所以评论) - 如果你被伪造者困住,伪造可能对你有用。 github.com/sevenwire/forgery
  • 谢谢迈克尔。我安装了 Forgery,具有讽刺意味的是,使用 Forgery 而不是 Faker,我仍然遇到同样的 2 个问题。它不会在“User_id”之后填充列,并且仍然会收到“nil:NilClass”错误的“未定义方法“名称”。也许它在我的模型中?
  • 好吧,我觉得自己很愚蠢..想通了。 - 我忘记将列添加到 attr_accessible: 在模型中 - 未定义的方法“名称”是模型中的太阳黑子块......一旦我修复它工作正常。希望这对某人有所帮助。

标签: ruby-on-rails sample-data faker


【解决方案1】:

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

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

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2019-08-13
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2019-07-10
    • 2013-08-08
    相关资源
    最近更新 更多