我的控制器里面有以下代码:
array = Contact.select(:name).distinct
我们的想法是,这将创建一个Contact具有唯一:name属性的所有模型的数组.但是,它抛出了这个错误:
NoMethodError (private method 'select' called for Contact:Class)
这里有什么误会?对于它的价值,调用这行代码的方法在控制器中没有被定义为私有.
这是实际的代码:
class FluidsurveysController < ApplicationController
def index
end
def import_contacts
@survey_provider = FluidsurveysProviders::SurveyProvider.new()
@current_month = Time.new.strftime("%B%Y%d")
fs_contact_list_array = csv_to_array(params[:file].tempfile)
@fs_contacts_array = []
fs_contact_list_array.each do |hash|
@fs_contacts_array << Contact.new(hash)
end
array = Contact.select(:name).distinct
end
end
Run Code Online (Sandbox Code Playgroud)
class Contact
include ActiveModel::Model
attr_reader :client_id, :client_name, :branch_id, :branch, :short_name, :unit_id, :membership_id,
:first_name, :last_name, :date_of_birth, :change_date_time, :membership_type,
:home_phone, :email_address, :anniversary_years
def initialize(fs_contact_hash = {})
@client_id = …Run Code Online (Sandbox Code Playgroud) 我有一个rake任务,它将按顺序调用另外4个rake任务:
rake:one
rake:two
rake:three
rake:four
Run Code Online (Sandbox Code Playgroud)
一个,两个和三个Rake任务正在获取数据并将其添加到我的数据库中.然后rake:four将对该数据做一些事情.但我需要确保首先完成一,二和三.每个rake任务实际上都是让Sidekiq工作人员在后台运行.在这种情况下,是否所有工人都rake:one先完成,然后rake:two等等?
如果没有,我如何确保工人按顺序执行?
我的脚本中有以下代码...
begin
#Loop to create 1000 emails...
#Loop to send 1000 emails...
rescue Timeout::Error => e
retry_attempts += 1
if retry_attempts < 10
retry
else
puts "Timeout error, deleting emails...".red
logs.puts("Rescued a timeout error...#{e}")
email_ids_all.each do |email_delete|
#delete all email...
end
Run Code Online (Sandbox Code Playgroud)
我的问题是retry实际上要"重试"的是什么.如果脚本已经在一个循环中生成了1000封电子邮件,并在另一个循环中发送了999封电子邮件,然后在发送第1000封电子邮件时超时 - 它是否会重试它遇到错误的特定代码行,是否会启动循环使用第1000个电子邮件,它会启动整个循环,还是从脚本开始运行两个循环开始?
我正在使用ruby 1.9.3.
我有一个数组:
array = ['Footballs','Baseball','football','Soccer']
Run Code Online (Sandbox Code Playgroud)
我需要计算看到足球或棒球的次数,无论情况和复数.
这是我试图做的,但没有运气:
array.count { |x| x.downcase.include? 'football' || x.downcase.include? 'baseball' }
Run Code Online (Sandbox Code Playgroud)
编写此代码的正确或更好的方法是什么?我正在寻找3作为答案.
我正在使用 ruby 2.0.0 和 Rails 4.0.0。我有类似的东西:
require 'net/sftp'
sftp = Net::SFTP.start('ftp.app.com','username', :password => 'password')
sftp.file.open("/path/to/remote/file.csv", "r") do |f|
puts f.gets
end
Run Code Online (Sandbox Code Playgroud)
这会在 FTP 站点上打开文件,但仅打开putscsv 文件的第一行。我需要逐行读取该文件,最好忽略标题。
如何在不将文件下载到本地的情况下逐行读取文件?
我正在尝试在我的Rails 4应用程序上设置Redis To Go.我希望能够将它部署到Heroku.
到目前为止,这就是我所做的:
通过该dashboard.heroku网站,我使用单击安装Nano版本的Redis To Go来安装我的应用程序的插件.
我添加gem 'redis'到我的gemfile中.
在config/environments/development.rb我添加这一行:
ENV["REDISTOGO_URL"] = 'redis://redistogo:b9fc604b1c86a1f6c232ce1dd16cd989@albacore.redistogo.com:10280/'
Run Code Online (Sandbox Code Playgroud)
然后,我创建了一个如下所示的config/initializers/redis.rb文件:
uri = URI.parse(ENV["redis://redistogo:b9fc604b1c86a1f6d872ce1dd16cd989@albacore.redistogo.com:10280/"] || "redis://localhost:6379/")
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Run Code Online (Sandbox Code Playgroud)
Redis在我的控制台中运行命令时,我收到此错误:
Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)
我在这里做错了什么,我需要做些什么来确保我可以在开发中测试并在没有任何问题的情况下部署到Heroku?
response 是一个哈希,看起来像两件事之一:
response = {'demo' => 'nil', 'test_01' => 'Demo Data'}
Run Code Online (Sandbox Code Playgroud)
要么
response = {'test' => 'Demo Data', 'demo' => 'nil'}
Run Code Online (Sandbox Code Playgroud)
我想做这样的事情:
if response.has_key? 'test_01'
new_response.update(:nps_score => response['test_01']
else
new_response.update(:nps_score => response['test']
end
Run Code Online (Sandbox Code Playgroud)
是否有更"红宝石"的方法?也许是使用||的东西 运营商?我正在使用ruby 2.0.0和rails 4.0.0.
我的控制器中有以下内容:
private
def csv_to_array(file)
csv = CSV::parse(File.open(file, 'r') {|f| f.read })
fields = csv.shift
csv.collect { |record| Hash[*fields.zip(record).flatten ] }
end
Run Code Online (Sandbox Code Playgroud)
它抛出了这个问题:
NameError (uninitialized constant FController::CSV):
app/controllers/f_controller.rb:27:in `csv_to_array'
app/controllers/f_controller.rb:9:in `import'
Run Code Online (Sandbox Code Playgroud)
这是我的理解,csv默认包含在ruby工具包中,因此不需要.是什么导致了这个问题?
我需要以2014-01-20最后两个星期日的格式计算.
所以,last_sunday = 2014-01-20和most_recent_sunday = 2014-01-26.
我使用的是Rails 4.0和Ruby 2.0.我怎样才能得到这些日期?
我有以下型号:
class Notification < ActiveRecord::Base
belongs_to :notification_path
before_save :set_default
def set_default
self.resolved = false unless self.resolved
end
end
Run Code Online (Sandbox Code Playgroud)
class NotificationPath < ActiveRecord::Base
has_many :notifications
end
Run Code Online (Sandbox Code Playgroud)
然后这段代码:
notification = Notification.new({"area"=>"test","severity"=>"10","message"=>"Test","notification_path_id"=> 3})
<Notification id: nil, area: "test", severity:
10, message: "Test", created_at: nil, updated_at: nil,
notification_path_id: 3, resolved: nil>
Run Code Online (Sandbox Code Playgroud)
notification.valid?
真正
notification.errors.full_messages
[]
因此,正如您所看到的 - 通知有效且没有错误.通知模型上的验证为零.但是,当我保存模型时:
notification.save
假
它不会保存.是什么造成的?可能值得注意的是,set_default运行成功,即使模型未保存,该resolved属性在尝试保存时也会设置为false.
下面的完整错误跟踪,在IRB中遵循以上内容时:
notification.save!ActiveRecord :: RecordNotSaved:ActiveRecord :: RecordNotSaved from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/persistence.rb:125:in
save!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/validations.rb:57:insave !来自/Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/attribute_methods/dirty.rb:29:insave!' from /Users/Jonathan/.rvm/gems/ruby-2.1.1@steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in …