我使用他们的cleardb mysql add on在heroku上托管了一个简单的rails应用程序。我在本地开发mysql。我在heroku上的生产数据库中播种数据时遇到问题。
我的种子数据在db / seeds.rb中看起来像这样。该应用程序正在使用faker和制作宝石。我有一个用户模型和一个人模型。
Fabricator(:user) do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
birthday { Faker::Date.backward(14000) }
phone_number { Faker::PhoneNumber.phone_number }
street { Faker::Address.street_address }
city { Faker::Address.city }
state { Faker::Address.state_abbr }
zip_code { Faker::Address.zip }
education { "Bachelor" }
image { File.open("app/assets/images/helloworld.jpg")}
end
Fabricator(:person) do
full_name { Faker::Name.name }
email { Faker::Internet.email }
image { File.open("app/assets/images/puppy.jpg")}
friend_test { 0 }
user
end
#40.times { Fabricate(:user)} #comment this out because running the below command will generate user already
40.times { Fabricate(:person)}
Person.create(full_name: "Ben Franklin", email: "ben@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)
Person.create(full_name: "George Washington", email: "george@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)
Person.create(full_name: "John Adam", email: "john@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 1)
Person.create(full_name: "Barack Obama", email: "barack@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)
Person.create(full_name: "John Kennedy", email: "john@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)
Person.create(full_name: "Andrew Jackson", email: "andrew@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 2)
Person.create(full_name: "Jimmy Carter", email: "jimmy@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 3)
Person.create(full_name: "Gerald Ford", email: "gerald@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 3)
Person.create(full_name: "Richard Nixon", email: "richard@gmail.com", friend_test: 0, image: File.open("app/assets/images/puppy.jpg"), user_id: 3)
最初我认为它没有正确播种,因为我没有正确清除数据库(因为heroku不能很好地使用mysql)。我完成了所有这些步骤,并且我认为我缩小了播种动作的问题。
heroku run rake db:drop
heroku run rake db:create
heroku run rake db:migrate
heroku run rake db:setup
heroku run rake db:seed #problem
当我跑
时heroku run rails console
并执行User.all
创建种子数据User_id = 1然后跳转到User_id = 11然后User_id = 21.这与Person模型相同。这是非常奇怪的,因为本地我在本地运行这些命令时,我只会得到1到40的用户ID,并且从1到40的person_id也是如此。我的本地数据库很好。我在视图中有逻辑,它取决于记录逐一进行,而不是随机进行。
也是rails 4.1.8
应用
自2014年12月8日起更新
是的我在本地和生产中使用mysql,这是我的database.yml文件
development:
adapter: mysql2
encoding: utf8
database: mini_facebook_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
staging:
adapter: mysql2
encoding: utf8
database: mini_facebook_staging
pool: 5
username: root
password:
socket: /tmp/mysql.sock
test:
adapter: mysql2
encoding: utf8
database: mini_facebook_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
并且是
gem 'mysql2', '~> 0.3.17'
我没有在这里设置生产。我正在使用heroku配置和heroku文档,无论如何都会生成database.yml。