我正在尝试为我的数据库播种,但我遇到了这个错误,并且作为初学者不知道如何解决它。我试图采集书签样本并将其分配给我的一些用户。任何想法为什么这会引发错误?这是错误:
vagrant@rails-dev-box:~/code/bookmarks$ rake db:seed
rake aborted!
NoMethodError: undefined method `sample' for #<Class:0xaa90cf4>
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.5/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/home/vagrant/code/bookmarks/db/seeds.rb:47:in `block (2 levels) in <top (required)>'
/home/vagrant/code/bookmarks/db/seeds.rb:46:in `times'
/home/vagrant/code/bookmarks/db/seeds.rb:46:in `block in <top (required)>'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.5/lib/active_record/relation/delegation.rb:13:in `each'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.5/lib/active_record/relation/delegation.rb:13:in `each'
/home/vagrant/code/bookmarks/db/seeds.rb:45:in `<top (required)>'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:223:in `load'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:223:in `block in load'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.0.5/lib/active_support/dependencies.rb:223:in `load'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/railties-4.0.5/lib/rails/engine.rb:540:in `load_seed'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.5/lib/active_record/tasks/database_tasks.rb:154:in `load_seed'
/home/vagrant/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.5/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
编辑:这是我的seeds.rb文件:
require 'faker'
# Create a User
# user = User.new(
# name: 'First Last',
# email: 'firstlast@gmail.com',
# password: 'password',
# )
# user.skip_confirmation!
# user.save
#Create Users
5.times do
user = User.new(
name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Lorem.characters(10)
)
user.skip_confirmation!
user.save!
end
users = User.all
#Create Bookmarks
10.times do
Bookmark.create!(
url: Faker::Internet.url
)
end
bookmarks = Bookmark.all
#Create Topics
10.times do
Topic.create!(
name: Faker::Lorem.sentence
)
end
topics = Topic.all
users.each do |user|
3.times do
user.bookmarks << Bookmark.sample
end
end
topics.each do |topic|
3.times do
user.topics << Topic.sample
end
end
puts "Seed finished"
puts "#{User.count} users created"
puts "#{Bookmark.count} bookmarks created"
puts "#{Topic.count} topics created"
提前感谢您的帮助!
答案 0 :(得分:4)
无论你在调用.sample
,它之前的对象显然都不是正确的对象类型。它应该是一个数组或者是&#39;集合&#39;查询数据库时会出现这种情况。像这样......
posts = Post.all
posts.sample
可以正常使用
任何不是数组(或类似数组)的对象都没有&#39;样本&#39;您可以使用的方法
答案 1 :(得分:3)
您无法在课程上调用sample
,因为这不是有效的方法。 sample
方法用于数组或ActiveRecord :: Relation数组。
例如,每个都会返回一个随机的
[0,1,2,3].sample
(0..10).to_a.sample
Post.all.sample
但是,这些例子之类的内容会产生错误。
User.sample
NoMethodError: undefined method 'sample' for #<Class:0x00000007faa378>
(9..199).sample
NoMethodError: undefined method 'sample' for 9..199:Range