当我发现种子文件有问题时,我正在用 Faker 的几千条记录为开发 postgres 数据库做种。我中止了种子操作并回滚了插入并修复了seeds.rb 文件。
当我再次运行它时,每个rake db:*任务都运行了两次。我可以跑得rake routes很好,但如果我跑了,rake db:drop我会得到这样的结果:
$ rake db:drop
Dropped database 'vp_development'
Dropped database 'vp_development'
Run Code Online (Sandbox Code Playgroud)
如果我尝试运行 migrate ,当它尝试应用索引时,整个事情就会崩溃,因为它已经创建了这些列。
我只有一个默认的 Rakefile,在 lib 或其他任何地方都没有自定义 rake 文件。
如果有任何区别,环境是 rails 5.0.0.1 和 ruby 2.2.2。我现在很迷茫。
这是我的 Rakefile
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require_relative 'config/application'
Rails.application.load_tas
Run Code Online (Sandbox Code Playgroud)
我已经看到其他线程暗示它可能是 gem 的问题,但是在这个问题开始的几天内我没有添加新的 gem。无论如何,这是gemfile。
source 'https://rubygems.org'
# Bundle edge Rails …Run Code Online (Sandbox Code Playgroud) 我的目标是将模式指定的一组文件复制到目标目录.源目录中的文件可以包含子目录.
我试过了:
cp_r(Dir.glob('**/*.html'), @target_dir):
Run Code Online (Sandbox Code Playgroud)
和
cp_r(FileList['**/*.html'], @target_dir):
Run Code Online (Sandbox Code Playgroud)
但都没有工作.
它只适用于我做的事情:
cp_r(Dir['.'], @target_dir):
Run Code Online (Sandbox Code Playgroud)
但我只需复制*.html文件而不是其他任何东西.
我需要什么
cp --parents
Run Code Online (Sandbox Code Playgroud)
命令呢
有没有使用现有Ruby/Rake方法的建议?
更新看起来像使用Ant更容易做的事情,使用Ruby/Rake堆栈是不可能的 - 可能我需要查看其他内容.我不想编写自定义代码以使其在Ruby中工作.我只是想到Ruby/Rake作为适当的解决方案.
更新2这是我用Ant做的
<target name="buildeweb" description="Builds web site" depends="clean">
<mkdir dir="${build.dir.web}" />
<copy todir="${build.dir.web}" verbose="true">
<fileset dir="${source.dir.web}">
<include name="**/*.html" />
<include name="**/*.htm" />
</fileset>
</copy>
<chmod perm="a+x">
<fileset dir="${build.dir.web}">
<include name="**/*.html" />
<include name="**/*.htm" />
</fileset>
</chmod>
</target>
Run Code Online (Sandbox Code Playgroud) 我遵循了Octopress文档中的所有说明:
sddhrthrt@thinkpad:~/octopress$ rake generate
## Generating Site with Jekyll
directory source/stylesheets/
create source/stylesheets/screen.css
Configuration from /home/sddhrthrt/octopress/_config.yml
/home/sddhrthrt/octopress/plugins/pygments_code.rb:5:
warning: already initialized constant PYGMENTS_CACHE_DIR
/home/sddhrthrt/octopress/plugins/render_partial.rb:25:in `require':
./plugins/octopress_filters.rb:69: undefined (?...) sequence:
/<div class="entry-content">(?<content>[\s\S]*?)<\/div>\s*<(footer|\/article)>/ (SyntaxError)
from /home/sddhrthrt/octopress/plugins/render_partial.rb:25
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:76:in `require'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:76:in `setup'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:75:in `each'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:75:in `setup'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:30:in `initialize'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/jekyll:224:in `new'
from /var/lib/gems/1.8/gems/jekyll-0.11.2/bin/jekyll:224
from /usr/local/bin/jekyll:19:in `load'
from /usr/local/bin/jekyll:19
Run Code Online (Sandbox Code Playgroud)
和
sddhrthrt@thinkpad:~/octopress$ rake preview
Starting to watch source with Jekyll and Compass. Starting Rack on port 4000
rake aborted!
undefined …Run Code Online (Sandbox Code Playgroud) 我已经知道这里有很多关于这个问题的线索,但是没有一个标题像我一样有这个措辞.我希望我们能够清除这一点.
$:.unshift(File.expand_path("../../lib", __FILE__))
Run Code Online (Sandbox Code Playgroud)
你可以在很多Ruby代码中看到这样的东西.无论这是否合适,意见都会有所不同.
我们可以对每个部分进行一些解释吗?真的很重要.我只了解其中一些.
这是我的主要rake文件
subrake = 'subrake'
task :init => [subrake] do
#call subrake.build
end
import subrake
Run Code Online (Sandbox Code Playgroud)
我看到有关上述步骤如何工作的文档,但我无法弄清楚如何在其他子文件中调用任务.顺便说一句,这些任务可能与我的名字相同,这是一个问题吗?
这是我的完整脚本.
我试图做一个rake任务,从目录中的文件收集数据并将它们加载到mysql中.
我修复了local-infile = 1,没什么用.它只是给我错误
namespace :db do
namespace :load do
desc "Load Properties into DB"
task :properties => :environment do
Mysql2::Client.default_query_options[:connect_flags] |= Mysql2::Client::LOCAL_FILES
@files = Dir.entries("db/property_website_scripts/")
connection = ActiveRecord::Base.connection()
for file in @files
next if file == "." || file == ".."
sql = "LOAD DATA LOCAL INFILE '#{Rails.root}/db/property_website_scripts/#{file}'
INTO TABLE properties
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\r\n'
(property_type,property_for,city,state,country......);"
connection.execute(sql)
end
#updating created at and updated at
Property.update_all({:created_at => Time.now, :updated_at => Time.now}, "created_at IS NULL")
end …Run Code Online (Sandbox Code Playgroud) 我在Gemfury上托管一个私有宝石,并希望覆盖rake release任务以推送到我的Gemfury私有URL而不是Rubygems.
我想保留Git标签的创建.
我应该开始的任何想法?
这是我的Rakefile的样子:
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task default: :spec
task test: :spec
Run Code Online (Sandbox Code Playgroud) 我正在尝试Hashie在 Rails 之外使用。在我的 rakefile 中,我已经包含了require hashie/hash,但我仍然得到NoMethodError. 我试过使用require hash; 那里也没有运气。
这是它失败的行:
YAML.load(ERB.new(File.read('../prefs.yml')).result)['dev'].symbolize_keys!
Run Code Online (Sandbox Code Playgroud)
当我inspect使用哈希时,它看起来是正确的并采用以下形式:{'key':'value'}. 我希望密钥是一个符号,但我不想在 Rails 3 和 4 之间切换,所以我安装Hashie了它并将其添加到我的 Rakefile 中,但这似乎并没有解决问题。
谁能告诉我为什么我可能会收到此错误?
我正在寻找一种能够检查Rakefile中是否存在某个rake任务的方法.我有一个任务依赖项,如果该任务可用,我只想将其作为依赖项包含在内.在这种特殊情况下,该任务仅在Rails项目中可用,但我希望我的rake任务也可以在更通用的Ruby应用程序环境中工作(不仅仅是Rails).
我想做这样的事情:
if tasks.includes?('assets:precompile')
task :archive => [:clean, :vendor_deps, 'assets:precompile']
...
end
else
task :archive => [:clean, :vendor_deps]
...
end
end
Run Code Online (Sandbox Code Playgroud)
在rake任务中有条件地包含任务依赖的最佳方法是什么?
我在我的environment.rb文件中定义了一个路径
MyRailsApp::Application.configure do
config.xml_import_path = "/path/to/xml"
end
Run Code Online (Sandbox Code Playgroud)
我希望能够像这样在Rake文件中设置变量访问
namespace :myapp do
xml_path = MyRailsApp::Application.config.xml_import_path
task :first_task => :environment do
# do some stuff with xml_path
end
task :second_task => :environment do
# do some other stuff with xml_path
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时我一直都会收到错误 rake myapp:first_task
undefined method `xml_import_path' for #<Rails::Application::Configuration:0x00000006a98bd8>
Run Code Online (Sandbox Code Playgroud)
我想这与环境没有被加载有关,因为它可以正常工作
xml_path = MyRailsApp::Application.config.xml_import_path
Run Code Online (Sandbox Code Playgroud)
在任务定义中.
是否有任何方法可以environment.rb像我试图那样使用设置的配置值?
有更好/正确的方法吗?
干杯,
卢克