我正在尝试将哈希插入到数组中,遵循以下示例:如何在ruby中创建动态多维数组?.什么地方出了错?
@array = Array.new
test1 = {"key1" => "value1"}
test2 = {"key2" => "value2"}
test3 = {"key3" => "value3"}
@array.push(0)
@array[0] << test1
# ERROR: can't convert Hash into Integer
@array[0] << test2
@array.push(1)
@array[1] << test2
@array[1] << test3
Run Code Online (Sandbox Code Playgroud) 我是 Rails、Rails_Admin 和 Devise 的新手。尝试在模型中获取 current_user,我认为应该由 Devise 提供:
class Item < ActiveRecord::Base
attr_accessible :user_id
belongs_to :user, :inverse_of => :items
after_initialize do
if new_record?
self.user_id = current_user.id unless self.user_id
end
end
end
Run Code Online (Sandbox Code Playgroud)
在 Rails_Admin 中我得到:
undefined local variable or method `current_user' for #<Item:0x007fc3bd9c4d60>
Run Code Online (Sandbox Code Playgroud)
与相同
self.user_id = _current_user.id unless self.user_id
Run Code Online (Sandbox Code Playgroud)
我看到 config/initializers/rails_admin.rb 中有一行,但不确定它的作用:
config.current_user_method { current_user } # auto-generated
Run Code Online (Sandbox Code Playgroud) 尝试在RailsAdmin中创建,查看或编辑记录时,hstore列不会显示.我假设RailsAdmin不支持这种Postgres数据类型,尽管它在Rails 4中本身支持.
有解决方法吗?
我很难访问Google Contacts API.
首先我尝试了google-api-ruby-clientgem,但事实证明它不支持Contacts API.
接下来的镜头是google_contacts_api 宝石,但我很难得到一个oauth_access_token_for_user与oAuth2 宝石.当遵循oAuth2指令时,我不知道放入什么authorization_code_value和Basic some_password.
我尝试了以下方法:
require 'oauth2'
client = OAuth2::Client.new(ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], :site => 'http://localhost:9292')
=> #<OAuth2::Client:0x007fcf88938758 @id="blabla.apps.googleusercontent.com", @secret="blabla", @site="http://localhost:9292", @options={:authorize_url=>"/oauth/authorize", :token_url=>"/oauth/token", :token_method=>:post, :connection_opts=>{}, :connection_build=>nil, :max_redirects=>5, :raise_errors=>true}>
client.auth_code.authorize_url(:redirect_uri => 'http://localhost:9292')
=> "http://localhost:9292/oauth/authorize?client_id=blabla.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A9292&response_type=code"
token = client.auth_code.get_token('authorization_code_value', :redirect_uri => 'http://localhost:9292', :headers => {'Authorization' => 'Basic some_password'})
=> Faraday::ConnectionFailed: Connection refused - connect(2) for "localhost" port 9292
Run Code Online (Sandbox Code Playgroud)
如果有人能给我详细的逐步说明如何访问API,我将不胜感激.
我按照这些说明检查用户在登录时是否已被软删除.在下面的示例中,我可以检查布尔值:
Class User < ActiveRecord::Base
def self.find_for_authentication(conditions)
super(conditions.merge(:deleted_flag => false))
end
Run Code Online (Sandbox Code Playgroud)
我更喜欢时间戳(created_at)字段.如何检查这样的字段是否为空?数据库为以下检查抛出错误:
super(conditions.merge(:deleted_at => false)) # also tried nil, "NULL" and "IS NULL"
Run Code Online (Sandbox Code Playgroud) 我们有一个辅助方法,用于在视图和通过 ActiveJob 发送的 SMS 中生成 URL。
我们如何访问作业中的辅助方法?
module Crew::AvailableJobsHelper
def accept_url(cleaner, occurrence)
if some_condition_is_true
crew_accept_job_url(cleaner_id: cleaner.id, job_id: occurrence.job.id)
else
crew_accept_occurrence_url(cleaner_id: current_user.id, occurrence_id: occurrence.id)
end
end
end
class Crew::UnassignedAlertJob < ActiveJob::Base
queue_as :default
def perform
sms_body = "Click here to accept a job: #{accept_url(cleaner, occurrence)}"
end
end
Run Code Online (Sandbox Code Playgroud) 我需要尽快批量处理大量文件(数百万条数据库记录).为此,我将文件拆分为3个目录,并使用标准配置(无配置文件)设置Sidekiq.
然后我启动了3个Heroku工作者并调用了3个方法,这些方法启动了3个Sidekiq工作程序,所有这些工作都具有"默认"队列.最初,Sidekiq使用了2名Heroku工人,过了一段时间后,它决定只使用1名工人.
我怎样才能迫使Sidekiq尽快使用所有3名工人完成工作?
谢谢

在本概述后,一个belongs_to :x, through: :y关系最好的委托方法来实现的.
是否有一个特殊的原因(技术原因,设计选择)为什么Rails不通过关系支持belongs_to?
class Division
belongs_to :league
has_many :teams
end
class Team
belongs_to :division
has_many :players
end
class Player
belongs_to :team
belongs_to :division, through: :team # WON'T WORK
end
Run Code Online (Sandbox Code Playgroud) 我有以下型号:
供应商有很多提交的商品
Submitted_prices有很多price_items
为了在没有submitted_prices和/或没有price_items时代码不崩溃,我曾经使用以下代码:
if supplier.submitted_prices.where(:purchaser_id => organization.id).last
&& supplier.submitted_prices.where(:purchaser_id => organization.id).last.price_items.where("item_id" => item.id).last
&& supplier.submitted_prices.where(:purchaser_id => organization.id).last.price_items.where("item_id" => item.id).last.unit_price.present?
order_item.unit_price = supplier.submitted_prices.where(:purchaser_id => organization.id).last.price_items.where("item_id" => item.id).last.unit_price
Run Code Online (Sandbox Code Playgroud)
然后我遇到了try():
order_item.unit_price = supplier.try(:submitted_prices).where(:purchaser_id => organization.id).try(:last).try(:price_items).where("item_id" => item.id).try(:last).try(:unit_price)
Run Code Online (Sandbox Code Playgroud)
问题是,我不知道如何在WHERE约束上运行try(),如果try返回nil,则此约束会失败:
NoMethodError Exception: undefined method `where' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
有没有办法围绕WHERE约束包装try()?
ruby ×3
rails-admin ×2
activerecord ×1
api ×1
devise ×1
google-api ×1
heroku ×1
postgresql ×1
sidekiq ×1