Rails:处理开发数据的最佳实践

发布于 2024-12-09 15:29:51 字数 417 浏览 0 评论 0原文

我有以下场景:

我正在开始开发一个长期项目(大约 6 个月),我需要一些有关数据库的信息才能测试我的功能。问题是,现在,我没有插入此信息的表单(将来我会),但我需要将信息加载到数据库上,处理此问题的最佳方法是什么?特别考虑到应用程序完成后,我将不再需要此过程。

举个例子,假设我有需要分类的任务。我已经开始处理这些任务,但我需要在我的数据库上加载一些类别。

顺便说一句,我正在使用 Rails 3.1。

提前致谢!

编辑

关于种子:有人告诉我,如果您的数据可能略有不同,则种子不是最佳选择,因为您必须删除所有信息并重新插入。说..我想更改或添加类别,然后我必须编辑seeds.rb文件,进行修改,然后删除并重新加载所有数据....,还有其他方法吗?或者种子确实是解决这个问题的最佳方法吗?

I have the following scenario:

I'm starting development of a long project (around 6 months) and I need to have some information on the database in order to test my features. The problem is that right now, I don't have the forms to insert this information (I will in the future) but I need the information loaded on the DB, what's the best way to handle this? Specially considering that once the app is complete, I won't need this process anymore.

As an example, lets say I have tasks that need to be categorized. I've begun working on the tasks, but I need to have some categories loaded on my db already.

I'm working with Rails 3.1 btw.

Thanks in advance!

Edit

About seeds:I've been told that seeds are not the way to go if your data may vary a bit, since you'd have to delete all information and reinsert it again. Say.. I want to change or add categories, then I'd have to edit the seeds.rb file, do my modifications and then delete and reload all data...., is there another way? Or are seeds the defenitely best way to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱要勇敢去追 2024-12-16 15:29:51

因此,听起来您可能会添加、更改或删除数据,这些数据将与其他数据混合在一起。所以seeds.rb 已经出局了。您需要使用的是迁移。这样,您就可以通过顺序过程搜索并识别要更改的数据,而迁移正是为此而设计的。否则,我认为最好的选择是通过 Rails 控制台手动更改数据。

编辑:一个很好的例子如下。

您正在使用 Capistrano 来处理您的部署。您想要将新类别“玩具”添加到您的系统中。在迁移文件中,您可以在迁移函数中添加 Category.create(:name => "Toys") 或类似的内容(我忘记了 Rails 3.1 中现在的名称,我知道有不过只有一个方法),在本地运行 rake db:migrate,测试您的更改,提交它们,然后如果可以接受,使用 cap:deploy 部署它,这将运行针对生产数据库进行新迁移,插入新类别,并使其可在已部署的应用程序中使用。

撇开这个例子不谈,这实际上取决于您的工作流程。如果您认为通过迁移添加新数据不会影响您的应用程序,那么就这样做吧。我想说 DHH(David Heinemeier Hansson)并不喜欢它,因为他严格使用它来随着时间的推移改变数据库的结构。如果您不知道 DHH 是 Rails 的创建者。

编辑2:
我刚刚想到的一个想法是,如果您对此感到不舒服,可以跳过使用迁移的概念。您可以 100% 依赖您的 db/seeds.rb 文件。当您想到“seeds.rb”时,您会想到创建信息,但情况不一定如此。您不是盲目地创建数据,而是可以检查相关数据是否已存在,如果存在则修改并保存它,如果不存在则只需简单地创建一个新记录。

db/seeds.rb

toys = Category.find_by_name("Toys")
if toys then
  toys.name = "More Toys"
  toys.save
else
  Category.create(:name => "More Toys")
end

运行rake db:seeds并且该代码将运行。您只需要在每次更改数据时一致更新 seeds.rb 文件,以便 1) 搜索正确的数据值,2) 更新正确的属性。

最后,执行此操作没有正确或错误的方法,这只是适合您和您的工作流程的方法。

So it sounds like you'll possibly be adding, changing, or deleting data along the way that will be intermingled amongst other data. So seeds.rb is out. What you need to use are migrations. That way you can search for and identify the data you want to change through a sequential process, which migrations are exactly designed for. Otherwise I think your best bet is to change the data manually through the rails console.

EDIT: A good example would be as follows.

You're using Capistrano to handle your deployment. You want to add a new Category, Toys, to your system. In a migration file then you would add Category.create(:name => "Toys") or something similar in your migration function (I forget what they call it now in Rails 3.1, I know there's only a single method though), run rake db:migrate locally, test your changes, commit them, then if it's acceptable deploy it using cap:deploy and that will run the new migration against your production database, insert the new category, and make it available for use in the deployed application.

That example aside, it really depends on your workflow. If you think that adding new data via migrations won't hose your application, then go for it. I will say that DHH (David Heinemeier Hansson) is not a fan of it, as he uses it strictly for changing the structure of the database over time. If you didn't know DHH is the creator of Rails.

EDIT 2:
A thought I just had, which would let you skip the notion of using migrations if you weren't comfortable with it. You could 100% rely on your db/seeds.rb file. When you think of "seeds.rb" you think of creating information, but this doesn't necessarily have to be the case. Rather than just blindly creating data, you can check to see if the pertinent data already exists, and if it does then modify and save it, but if it doesn't exist then just create a new record plain and simple.

db/seeds.rb

toys = Category.find_by_name("Toys")
if toys then
  toys.name = "More Toys"
  toys.save
else
  Category.create(:name => "More Toys")
end

Run rake db:seeds and that code will run. You just need to consistently update the seeds.rb file every time you change your data, so that 1) it's searching for the right data value and 2) it's updating the correct attributes.

In the end there's no right or wrong way to do this, it's just whatever works for you and your workflow.

南街九尾狐 2024-12-16 15:29:51

加载开发数据的位置是db/seeds.rb。例如,由于您可以在那里编写任意 Ruby 代码,因此您甚至可以从外部文件加载开发数据。

The place to load development data is db/seeds.rb. Since you can write arbitrary Ruby code there, you can even load your dev data from external files, for instance.

不疑不惑不回忆 2024-12-16 15:29:51

有一个名为 db/seeds.rb 的文件,

您可以使用它实例化记录

user1=User.create(:email=>"[email protected]", 
  :first_name=>"user", 
  :last_name=>"name", 
  :bio=>"User bio...", 
  :website=>"http://www.website.com", 
  :occupation=>"WebDeveloper",
  :password=>"changeme", 
  :password_confirmation=>"changeme", 
  :avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
  )
user2=User.create(:email=>"[email protected]", 
  :first_name=>"user2", 
  :last_name=>"name2", 
  :bio=>"User2 bio...", 
  :website=>"http://www.website.com", 
  :occupation=>"WebDeveloper",
  :password=>"changeme", 
  :password_confirmation=>"changeme", 
  :avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
  )

只需从命令行运行 rake db:seed 即可将其放入数据库

there is a file called db/seeds.rb

you can instantiate records using it

user1=User.create(:email=>"[email protected]", 
  :first_name=>"user", 
  :last_name=>"name", 
  :bio=>"User bio...", 
  :website=>"http://www.website.com", 
  :occupation=>"WebDeveloper",
  :password=>"changeme", 
  :password_confirmation=>"changeme", 
  :avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
  )
user2=User.create(:email=>"[email protected]", 
  :first_name=>"user2", 
  :last_name=>"name2", 
  :bio=>"User2 bio...", 
  :website=>"http://www.website.com", 
  :occupation=>"WebDeveloper",
  :password=>"changeme", 
  :password_confirmation=>"changeme", 
  :avatar => File.open(File.join(Rails.root, '/app/assets/images/profiles/image.png'))
  )

Just run rake db:seed from command line to get it into the db

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文