将默认值和索引添加到 Rails 模板中的脚本/生成命令?

发布于 2024-08-04 20:00:10 字数 1671 浏览 1 评论 0原文

我正在尝试设置一个 Rails 模板,该模板允许对特定 Rails 应用程序进行全面设置。使用 Pratik Naik 的概述 (http://m.onkey.org/2008/12 /4/rails-templates),我能够设置几个脚手架和模型,其中一条线看起来像这样......

generate("scaffold", "post", "title:string", "body:string")

我现在尝试添加延迟作业,通常有一个看起来像这样的迁移文件:

create_table :delayed_jobs, :force => true do |table|
  table.integer  :priority, :default => 0      # Allows some jobs to jump to the front of the queue
  table.integer  :attempts, :default => 0      # Provides for retries, but still fail eventually.
  table.text     :handler                      # YAML-encoded string of the object that will do work
  table.text     :last_error                   # reason for last failure (See Note below)
  table.datetime :run_at                       # When to run. Could be Time.now for immediately, or sometime in the future.
  table.datetime :locked_at                    # Set when a client is working on this object
  table.datetime :failed_at                    # Set when all retries have failed (actually, by default, the record is deleted instead)
  table.string   :locked_by                    # Who is working on this object (if locked)
  table.timestamps
end

所以,我试图用 Rails 模板做的是添加 :default =>; 0 到主模板文件中。我知道模板命令的其余部分应该如下所示:

generate("migration", "createDelayedJobs", "priority:integer", "attempts:integer", "handler:text", "last_error:text", "run_at:datetime", "locked_at:datetime", "failed_at:datetime", "locked_by:string")

我将在哪里放置(或者更确切地说,添加的语法是什么) :default 值?如果我想添加索引,最好的方法是什么?

I'm trying to set up a Rails Template that would allow for comprehensive set-up of a specific Rails app. Using Pratik Naik's overview (http://m.onkey.org/2008/12/4/rails-templates), I was able to set up a couple of scaffolds and models, with a line that looks something like this ...

generate("scaffold", "post", "title:string", "body:string")

I'm now trying to add in Delayed Jobs, which normally has a migration file that looks like this:

create_table :delayed_jobs, :force => true do |table|
  table.integer  :priority, :default => 0      # Allows some jobs to jump to the front of the queue
  table.integer  :attempts, :default => 0      # Provides for retries, but still fail eventually.
  table.text     :handler                      # YAML-encoded string of the object that will do work
  table.text     :last_error                   # reason for last failure (See Note below)
  table.datetime :run_at                       # When to run. Could be Time.now for immediately, or sometime in the future.
  table.datetime :locked_at                    # Set when a client is working on this object
  table.datetime :failed_at                    # Set when all retries have failed (actually, by default, the record is deleted instead)
  table.string   :locked_by                    # Who is working on this object (if locked)
  table.timestamps
end

So, what I'm trying to do with the Rails template, is to add in that :default => 0 into the master template file. I know that the rest of the template's command should look like this:

generate("migration", "createDelayedJobs", "priority:integer", "attempts:integer", "handler:text", "last_error:text", "run_at:datetime", "locked_at:datetime", "failed_at:datetime", "locked_by:string")

Where would I put (or, rather, what is the syntax to add) the :default values in that? And if I wanted to add an index, what's the best way to do that?

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

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

发布评论

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

评论(1

捂风挽笑 2024-08-11 20:00:10

我不认为可以将额外的信息传递到模板生成调用中。模板系统并不是特别广泛。当然,我可能是错的...另一种选择可能是将您想要的行注入到生成的迁移文件中。看看 http://github.com/cyberkni/super_stack/blob/master /templates/ 在 helper.rb 文件中了解执行此操作的方法,并在 mobile.rb 文件中了解使用它的示例。

I don't think it is possible to pass that extra information into the template generate calls. The template system isn't particularly extensive. I could be wrong of course... The other option might be to just inject the line you are going to want into the generated migration file instead. Take a look at http://github.com/cyberkni/super_stack/blob/master/templates/ in the helper.rb file for a way to do that, and at the mobile.rb file for an example of using it.

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