[英]Generate a model that uses pre-existing data from database
Im making an application with a pre-existing database, I set up the database.yml to use the database. 我正在使用预先存在的数据库制作应用程序,我将database.yml设置为使用该数据库。
database.yml 数据库
development:
adapter: mysql2
encoding: utf8
# database: ttlem_demo_development
database: ttle
pool: 5
username: root
password:
socket: /tmp/mysql.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
database: ttlem_demo_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql2
encoding: utf8
database: ttlem_demo_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
I only want one table out of the database it is called account views, I try to generate a scaffold for this with all the correct fields but it tells me i need to migrate (when i render it in the browser), if i migrate i wont be able to use the existing data, is this correct? 我只想从数据库中取出一个表,称为帐户视图,我尝试为此生成一个具有所有正确字段的支架,但是它告诉我,如果我进行迁移,则需要迁移(在浏览器中呈现时)将无法使用现有数据,对吗? How can i make a model that will use the existing data and fields?
我如何制作一个将使用现有数据和字段的模型?
Thank you for all your help :) 谢谢你的帮助 :)
Two things to try:... #1 Run your scaffold without a migration so it doesn't think you're missing one. 可以尝试两件事:...#1在不进行迁移的情况下运行您的脚手架,这样就不会丢失您的脚手架。
rails g scaffold yourmodelname fieldone:fieldtype ... etc --no-migration
#2 If that doesn't work you can go the long way round but dumping and reloading with a valid schema version number #2如果这不起作用,则可以走很长一段路,但要使用有效的架构版本号进行转储和重新加载
Add this db to yml to your gemfile: 将此数据库添加到yml到您的gemfile中:
gem 'yaml_db', github: 'jetthoughts/yaml_db', ref: 'fb4b6bd7e12de3cffa93e0a298a1e5253d7e92ba'
It works for either rails 3 or rails 4. 它适用于导轨3或导轨4。
Do a schema dump of your current database so you'll get a schema.rb with a valid version number. 对当前数据库进行模式转储,以便获得带有有效版本号的schema.rb。
bundle exec rake db:schema:dump
Now that you have a valid schema dump your data. 现在您已经有了一个有效的架构,可以转储数据。
bundle exec rake db:data:dump
Drop your database (you can do it manually using the mysql commands if you prefer or run rake db:drop) 删除数据库(如果愿意,可以使用mysql命令手动进行操作,也可以运行rake db:drop)
Now recreate it with your schema file. 现在,使用您的架构文件重新创建它。
bundle exec rake db:schema:load
Now add back your data 现在添加回您的数据
bundle exec rake db:data:load
Start your server and assuming your correctly matched all your data fields in your model (so the proper strong parameters are set from your scaffold) you should be good. 启动服务器,并假设您正确匹配了模型中的所有数据字段(因此可以从支架中设置适当的强参数),您应该会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.