我在Rails项目中测试一个gem.
当前的Gemfile:
gem 'mygemname', path: '/path/to/my/gem'
Run Code Online (Sandbox Code Playgroud)
当我在本地编辑gem时,我可以构建gem,从中删除gem Gemfile,运行bundle install,将gem添加回Gemfile并bundle install再次运行.在本地有更简单的方法吗?
我在线阅读数据,但它是字符串格式.我怎样才能使它返回一个JSON对象.
示例数据读取:
text = '{"one":1,"two":2}'
Run Code Online (Sandbox Code Playgroud)
转换示例:
data = JSON.parse(text).to_json
Run Code Online (Sandbox Code Playgroud)
但当我这样做时:
puts data.class
#=> String
Run Code Online (Sandbox Code Playgroud) 我想在我的.html.erb视图中打印以下字符串.
<%= selected_color %>
Run Code Online (Sandbox Code Playgroud)
所以,我尝试在我的文件中关注:
<h3><%= '<%= selected_color %>' %></h3>
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:
syntax error, unexpected $undefined, expecting ')'
Run Code Online (Sandbox Code Playgroud)
如果我删除<%= '<%= selected_color %>' %>并放置一些字符串abc,那么它将打印在页面上没有任何问题.
由于它是静态页面,我不想为它创建一个控制器.那么,你可以帮助在页面上显示该字符串吗?
ps我正在使用ruby v1.9.3和rails v3.1.0
以下命令在拉出具有特定功能的所有样式时非常有用:
Style.joins(:style_features).where('style_features.feature_id= ?', 1)
Run Code Online (Sandbox Code Playgroud)
有可能做同样的事情,但对于一系列功能?如:
Style.joins(:style_features).where('style_features.feature_id= ?', [1, 2, 3])
Run Code Online (Sandbox Code Playgroud) 我正在使用Rails 4.1构建API.我的一个调用需要2个输入字段,并调用第三方API以获取更多数据.然后,它使用该数据制作ActiveRecord模型.
我该如何验证输入?我不是从2个输入字段制作模型.
注意:在调用第三方API之前,需要对它们进行验证
我已经构建了一些serverspec代码来在多个主机上运行一组测试.问题是当任何测试失败时,测试在当前主机上停止.即使测试失败,我希望它继续所有主机.
Rakefile:
namespace :spec do
task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
hosts.each do |host|
begin
desc "Run serverspec to #{host}"
RSpec::Core::RakeTask.new(host) do |t|
ENV['TARGET_HOST'] = host
t.pattern = "spec/cfengine3/*_spec.rb"
end
rescue
end
end
end
Run Code Online (Sandbox Code Playgroud)
完整代码:https: //gist.github.com/neilhwatson/1d41c696102c01bbb87a
我试图通过使用另一个具有默认值的哈希来初始化 ruby 中的哈希。我想要一个深副本,但我似乎只得到一个浅副本。
这是一个例子:
DEFAULT_HASH = { a: 0, b: 1 }.freeze
my_hash = DEFAULT_HASH.dup
my_hash[:a] = 4
Run Code Online (Sandbox Code Playgroud)
现在“my_hash”和 DEFAULT_HASH 中的 a 值是 4。我只希望哈希中的值发生变化。
我也尝试过其他方法:
my_hash = {}.merge DEFAULT_HASH
Run Code Online (Sandbox Code Playgroud)
和
my_hash.merge! DEFAULT_HASH
Run Code Online (Sandbox Code Playgroud)
所有这些都会产生相同的效果。实现这种初始化的最佳方法是什么?我还使用嵌套哈希,这增加了一点复杂性。
即我的 DEFAULT_HASH 看起来像:
DEFAULT_HASH = { a:{a:1, b:2}, b:{a:2, b:1} }
Run Code Online (Sandbox Code Playgroud)
这会影响如何做到这一点吗?
编辑:嵌套哈希案例
DEFAULT_HASH = { a:{a:1, b:2}, b:{a:2, b:1} }
=> {:a=>{:a=>1, :b=>2}, :b=>{:a=>2, :b=>1}}
a=DEFAULT_HASH.dup
=> {:a=>{:a=>1, :b=>2}, :b=>{:a=>2, :b=>1}}
a[:b][:a]=12
=> 12
DEFAULT_HASH
=> {:a=>{:a=>1, :b=>2}, :b=>{:a=>12, :b=>1}}
Run Code Online (Sandbox Code Playgroud) 在我的Rails应用程序中,使用PostgreSQL数据库,我有一个如下所示的表:
create_table "person_identifiers", force: :cascade do |t|
t.string "identifier", limit: 255
end
Run Code Online (Sandbox Code Playgroud)
我想在该列上添加索引.我通过以下迁移执行此操作:
execute('create index person_identifiers_identifier on person_identifiers using gin (identifier gin_trgm_ops)')
Run Code Online (Sandbox Code Playgroud)
这是按预期工作所以在模式中我有以下索引:
add_index "person_identifiers", ["identifier"], name: "person_identifiers_identifier", using: :gin
Run Code Online (Sandbox Code Playgroud)
但我希望这个索引不区分大小写,所以我写道:
execute('create index person_identifiers_identifier on person_identifiers using gin (lower(identifier) gin_trgm_ops)')
Run Code Online (Sandbox Code Playgroud)
但遗憾的是在schema.rb中看不到?我知道我可以使用SQL格式而不是schema.rb,但我想坚持在schema.rb中看到这一点.
除了命令行实用程序提供的 oneliner 之外,我找不到任何文档。它说:
-batch force batch I/O
Run Code Online (Sandbox Code Playgroud)
那么,如果我向它传递一个查询或多个查询,这里会发生什么?
我已经通过数据库迁移到has_many:通过帖子,类别和分类关系之间的关联.
架构:
create_table "categories", force: :cascade do |t|
t.string "title"
t.integer "subscribers"
t.integer "mod"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "categorizations", force: :cascade do |t|
t.integer "category_id"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.text "content"
t.integer "category_id"
end
Run Code Online (Sandbox Code Playgroud)
楷模:
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :post
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many …Run Code Online (Sandbox Code Playgroud) ruby ×7
sql ×2
bundler ×1
database ×1
gem ×1
hash ×1
json ×1
postgresql ×1
serverspec ×1
sqlite ×1
validation ×1