标签: ruby

从迁移中执行Rake任务?

我有一个Rake任务,可以从文件中将配置数据加载到DB中,是否有正确的ruby/rails方式在迁移时调用它?

我的目标是同步我的团队数据库配置,而不必广播然后运行任务 lalala

  def self.up
    change_table :fis_situacao_fiscal do |t|
      t.remove :mostrar_endereco
      t.rename :serie, :modelo 
    end

    Faturamento::Cfop.destroy_all()
    #perform rake here !
  end
Run Code Online (Sandbox Code Playgroud)

更新 我现在的工作方式,并且有效:

system('rake sistema:load_data file=faturamento/cfop')
Run Code Online (Sandbox Code Playgroud)

这是@Ryan Bigg的建议,它是例外:

Rake::Task['rake sistema:load_data file=faturamento/cfop'].invoke()
Run Code Online (Sandbox Code Playgroud)

.

==  AlterSituacaoFiscalModeloEndereco: migrating ====================
-- change_table(:fis_situacao_fiscal)
   -> 0.0014s

rake aborted!
An error has occurred, this and all later migrations canceled:

Don't know how to build task 'rake sistema:load_data file=faturamento/cfop'
Run Code Online (Sandbox Code Playgroud)

哪里出错了?

ruby migration ruby-on-rails

50
推荐指数
3
解决办法
2万
查看次数

尽管有SSH密钥,Capistrano在部署时会要求输入密码

我的ssh密钥肯定设置正确,因为我在使用ssh时从未提示输入密码.但是在部署时,capistrano仍然要求输入密码cap deploy.我设置的时候不会要求密码cap deploy:setup,奇怪的是.如果没有密码提示,它将使部署周期更加顺畅.

细节:我正在将一个Sinatra应用程序部署到Dreamhost共享帐户(使用Passenger).我曾经按照教程做了很长时间,这在当时完美无缺.从那以后出现了什么.我正在使用capistrano(2.5.9)和git版本1.6.1.1.这是我的Capfile:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator

set :user, 'ehsanul'
set :domain, 'jellly.com'

default_run_options[:pty] = true

# the rest should be good
set :repository,  "ehsanul@jellly.com:git/jellly.git"
set :deploy_to, "/home/ehsanul/jellly.com"
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'deploy'
set :git_shallow_clone, 1
set :scm_verbose, true
set :use_sudo, false

server domain, :app, :web

namespace :deploy do
  task :migrate do
    run "cd #{current_path}; /usr/bin/rake migrate environment=production"
  end
  task :restart do
    run "touch #{current_path}/tmp/restart.txt"
  end
end

after …
Run Code Online (Sandbox Code Playgroud)

ruby capistrano ruby-on-rails sinatra ssh-keys

50
推荐指数
3
解决办法
4万
查看次数

Ruby on Rails 3如何使'OR'条件

我需要一个SQL语句来检查是否满足一个条件:

SELECT * FROM my_table WHERE my_table.x=1 OR my_table.y=1
Run Code Online (Sandbox Code Playgroud)

我想以'Rails 3'的方式做到这一点.我在寻找类似的东西:

Account.where(:id => 1).or.where(:id => 2)
Run Code Online (Sandbox Code Playgroud)

我知道我总是可以回退到sql或条件字符串.但是,根据我的经验,这在组合范围时经常会导致混乱.做这个的最好方式是什么?

另一个相关的问题是,如何描述依赖于OR条件的关系.我找到的唯一方法:

has_many :my_thing, :class_name => "MyTable",  :finder_sql => 'SELECT my_tables.* ' + 'FROM my_tables ' +
'WHERE my_tables.payer_id = #{id} OR my_tables.payee_id = #{id}'
Run Code Online (Sandbox Code Playgroud)

然而,当组合使用时,这些再次破裂.有没有更好的方法来指定这个?

ruby ruby-on-rails ruby-on-rails-3

50
推荐指数
6
解决办法
6万
查看次数

Ruby on Rails:如何在Rails中明确定义复数名称和单数名称?

例如,我使用"奖金"作为我的模型,所以我希望"奖金"是复数形式,"奖金"是单数形式.

但是,在Ruby中,这导致:

"bonus".pluralize # bonus
"bonuses".singularize # bonuse
Run Code Online (Sandbox Code Playgroud)

因此,当我执行"has_many:bonus"时,它不使用Bonus.rb模型(因为Ruby需要Bonuse.rb模型).有没有办法在Ruby on Rails中以某种方式纠正它,使得"奖金"充当模型bonus.rb的复数形式?

ruby ruby-on-rails

50
推荐指数
1
解决办法
2万
查看次数

使用Ruby按行读取,编辑和编写文本文件

有没有一种在Ruby中读取,编辑和编写文件的好方法?

在我的在线搜索中,我发现了一些建议将其全部读入数组的内容,修改所述数组,然后将所有内容写出来.我觉得应该有一个更好的解决方案,特别是如果我正在处理一个非常大的文件.

就像是:

myfile = File.open("path/to/file.txt", "r+")

myfile.each do |line|
    myfile.replace_puts('blah') if line =~ /myregex/
end

myfile.close
Run Code Online (Sandbox Code Playgroud)

replace_puts将当前行上书写,而不是(上)写的下一行,因为它目前确实因为指针位于线(分离后)结束.

那么匹配的每一行/myregex/都会被'blah'取代.显然我想到的是涉及到的更多,就处理而言,并且将在一行中完成,但想法是一样的 - 我想逐行读取文件,并编辑某些行,以及我完成后写出来.

也许有一种方法可以说"倒回到最后一个分隔符之后"?或者each_with_index通过线索引号使用和写入某种方式?但是,我找不到任何类似的东西.

到目前为止,我所拥有的最佳解决方案是按顺序读取内容,将它们逐行写入新的(临时)文件(可能已编辑),然后使用新的临时文件覆盖旧文件并删除.同样,我觉得应该有更好的方法 - 我认为我不应该创建一个新的1gig文件只是为了编辑现有1GB文件中的某些行.

ruby io file

50
推荐指数
2
解决办法
4万
查看次数

如何传递函数而不是块

可能重复:
将数组的每个元素传递给函数的更短方式

我知道这会奏效:

def inc(a)
  a+1
end
[1,2,3].map{|a| inc a}
Run Code Online (Sandbox Code Playgroud)

但在Python中,我只需要写:

map(inc, [1,2,3])
Run Code Online (Sandbox Code Playgroud)

要么

[inc(x) for x in [1,2,3])
Run Code Online (Sandbox Code Playgroud)

我想知道我是否可以跳过在Ruby中创建块的步骤,并且这样做:

[1,2,3].map inc
# => ArgumentError: wrong number of arguments (0 for 1)
# from (irb):19:in `inc'
Run Code Online (Sandbox Code Playgroud)

有没有人有关于如何做到这一点的想法?

ruby functional-programming

50
推荐指数
2
解决办法
2万
查看次数

如何在Ruby中将字符串转换为UTF8

我正在写一个使用Hpricot的爬虫.它从某个网页下载一个字符串列表,然后我尝试将其写入该文件.编码有问题:

"\xC3" from ASCII-8BIT to UTF-8
Run Code Online (Sandbox Code Playgroud)

我有在网页上呈现并以这种方式打印的项目:

Développement
Run Code Online (Sandbox Code Playgroud)

str.encoding回报UTF-8,所以force_encoding('UTF-8')没有帮助.我怎么能把它转换成可读的UTF-8?

ruby encoding dump file utf-8

50
推荐指数
3
解决办法
8万
查看次数

当我尝试使用user.save时,为什么我的rails会回滚?

我已经安装了RailsTutorial示例应用程序(类似应用程序的推特),并且我试图理解为什么当我尝试更新用户数据库时,下面的控制台代码不会更新数据库.我期待用户信息在我使用后得到更新user.save.但是,这会回滚到未经编辑的数据.这是由于基于用户的限制吗?

用户控制器:

class UsersController < ApplicationController

#before_filter :signed_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]
# By default before filters apply to all actions
#before_filter :correct_user, only: [:edit, :update]
  def edit
    @user = User.find(params[:id])
  end

  def update

    @user = User.find params[:id]

    respond_to do |format|

    if @user.update_attributes(params[:user])
      flash[:notice] = "Profile updated"
      format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
      format.json { respond_with_bip(@user) }
    else

      format.html { render :action => "edit" }
      format.json { respond_with_bip(@user) }
    end
    end …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails railstutorial.org

50
推荐指数
5
解决办法
5万
查看次数

在RubyMine中运行规范会导致"无法加载此类文件 - teamcity/spec/runner/formatter/teamcity/formatter(LoadError)"

操作系统:Arch Linux,Rails版本:4,RubyMine:6.3

当我从工具运行规范 - 运行Rake任务 - 规范我总是得到这个错误:

/home/chylli/.rvm/gems/ruby-2.1.2@rails4/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1024:in`requirement':无法加载此类文件 - - teamcity/spec/runner/formatter/teamcity/formatter(LoadError)

但运行'debug spec:models'是可以的.

我尝试了什么:我在Run - edit配置下添加了'ruby arguments':

-I $ RUBYMINE_HOME/rb/testing/patch/bdd -I $ RUBYMINE_HOME/rb/testing/patch/common

但它不起作用.

ruby rspec ruby-on-rails rubymine ruby-on-rails-4

50
推荐指数
2
解决办法
7940
查看次数

严格将字符串转换为整数(或零)

对于Web编程,数字以字符串形式出现.但to_i将其转换"5abc"5"abc"0,这两个错误的答案.为了抓住这些,我写道:

def number_or_nil( s )
  number = s.to_i
  number = nil if (number.to_s != s)
  return number
end
Run Code Online (Sandbox Code Playgroud)

有没有更整洁,更自然的方式来实现这种转换,并检测到字符串不是一个数字?

ruby type-conversion

50
推荐指数
2
解决办法
7万
查看次数