在Rails查询中包含LIKE子句的最佳方法是什么,即(完全不正确的):
Question.where(:content => 'LIKE %farming%')
Run Code Online (Sandbox Code Playgroud) 我通过典型的ORM设置使CarrierWave正常工作并通过表单上传.我想弄清楚如何在表单提交上下文之外使用CarrierWave.例如,当用户注册时,我想抓住他们的gravatar并将其存储在CarrierWave中.这是我拥有的,它不起作用:
gravatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.new.update(current_user.email)}?s=512&d=identicon"
uploader = ImageUploader.new
uploader.store! gravatar_url
Run Code Online (Sandbox Code Playgroud)
也没有错误.我一直在寻找网络,但一直无法找到解决方案.
我希望能够使用Ruby从内部调用匿名lambda.考虑以下递归块(返回阶乘).我知道我可以将它分配给变量,并且该变量在lambda的范围内:
fac = lambda { |n| n == 1 ? 1 : n * fac.call(n - 1) }
fac.call(5)
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够做到以下(因为没有实际原因,我只是对探索语言感兴趣):
(lambda { |n| n == 1 ? 1 : n * self.call(n - 1) }).call(5)
Run Code Online (Sandbox Code Playgroud)
我知道那self
是行不通的,因为是main
对象.我做错了吗?我试图做一些不可能的事情 - 如果不是,这是因为某些理论上的限制还是仅仅是在Ruby中没有实现?
我知道如何使用RVM,但现在我有一个奇怪的问题,我不明白为什么.
这是一个简单的故事(我使用的是Ubuntu):
我创建了一个Rails项目,该项目的目标是"bookstore /".
我转到项目目录cd bookstore
,然后输入rvm list
如下命令:
bookstore/$ rvm list
rvm rubies
ruby-1.9.2-p136 [ i386 ]
ruby-1.8.7-p352 [ i386 ]
ruby-1.8.7-p330 [ i386 ]
ruby-1.8.6-p420 [ i386 ]
ruby-1.9.2-p290 [ i386 ]
Run Code Online (Sandbox Code Playgroud)
由于我没有看到=>
应该指示当前正在使用的ruby版本的箭头符号,因此我使用以下RVM命令指定ruby版本:
bookstore/$ rvm use ruby-1.9.2-p290
Using /home/usr/.rvm/gems/ruby-1.9.2-p290
Run Code Online (Sandbox Code Playgroud)
现在,如果我rvm list
看到我的项目正在使用ruby v1.9.2:
bookstore/$ rvm list
rvm rubies
ruby-1.9.2-p136 [ i386 ]
ruby-1.8.7-p352 [ i386 ]
ruby-1.8.7-p330 [ i386 ]
ruby-1.8.6-p420 [ i386 ]
=> ruby-1.9.2-p290 [ i386 ] …
Run Code Online (Sandbox Code Playgroud) 我使用has_many有以下问题:通过collection_select在multi-select中通过多对多关系:multiple => true.我有供应商提供许多成分,可由许多供应商提供.看一看:
成分模型:
class Ingredient < ActiveRecord::Base
has_many :ingredient_suppliers
accepts_nested_attributes_for :ingredient_suppliers, :allow_destroy => true
has_many :suppliers, :through => :ingredient_suppliers
end
Run Code Online (Sandbox Code Playgroud)
供应商模型:
class Supplier < ActiveRecord::Base
has_many :ingredient_suppliers
has_many :ingredients, :through => :ingredient_suppliers
end
Run Code Online (Sandbox Code Playgroud)
关系实体:
class IngredientSupplier < ActiveRecord::Base
belongs_to :ingredient
belongs_to :supplier
end
Run Code Online (Sandbox Code Playgroud)
这就是形式.请注意,如果不指定:name,我无法使其工作:
<%= form_for(@ingredient) do |f| %>
<%= f.fields_for :suppliers do |supplier_fields| %>
<%= supplier_fields.collection_select (:supplier_ids,
Supplier.all(:order=>"name ASC"),
:id, :name,
{:selected => @ingredient.supplier_ids,
:include_blank => true},
{:multiple => true,
:name => 'ingredient[supplier_ids]'}) %>
<% end %>
<% …
Run Code Online (Sandbox Code Playgroud) 我有一个代表航班规则的域模型.飞行规则是执行航班的一系列法规.视觉飞行规则或VFR就是这样一个例子.我试图建立这个模型,但是Rails坚持使用FlightRules
singular(FlightRule
),而我需要它保持复数.是否有任何方法让Rails保持模型名称复数?
我一直在努力掌握编写测试的方法,但是由于测试似乎永远无法验证我想要的方式,因此我遇到了很多麻烦.特别是,我一直在尝试使用Factory Girl而不是固定装置 - 正如我最近在网上看到的Railscasts和其他建议所示 - 由于它所宣称的好处,而且还没有成功.
例如,这是一个用户模型的简单Rspec测试,测试以确保存在用户名...
describe User do
it "should not be valid without a username" do
user = FactoryGirl.create(:user, :username => "", :password => "secret")
user.should_not be_valid
end
end
Run Code Online (Sandbox Code Playgroud)
和我的factories.rb文件,如果它有帮助...
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "registered-#{n}" }
password "foobar"
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行'rake spec'时,它告诉我......
1) User should not be valid without a username
Failure/Error: user = FactoryGirl.create(:user, :username => "", :password => "secret")
ActiveRecord::RecordInvalid:
Validation failed: Username can't be blank
Run Code Online (Sandbox Code Playgroud)
嗯......这就是POINT.如果我指定用户不应该有效,那么这个测试真的不应该通过吗?
如果我更换了Factory Girl系列并在测试中设置了类似的用户'user = …
我有一个应用程序,我最近更新到Rails 3.2.1(来自Rails 3.0.x)并重构了JS和CSS资产以利用新的资产管道.该应用程序托管在Heroku上,带有Celadon Cedar堆栈.
我将特定于应用程序的配置保存在名为app_config.yml的YAML文件中,并使用初始化程序将其加载到全局APP_CONFIG变量中:
# config/initializers/load_app_config.rb
app_config_contents = YAML.load_file("#{Rails.root.to_s}/config/app_config.yml")
app_config_contents["default"] ||= {}
APP_CONFIG = app_config_contents["default"].merge(
app_config_contents[Rails.env] || {} ).symbolize_keys
Run Code Online (Sandbox Code Playgroud)
Heroku支持内置于Cedar堆栈的Rails资产管道.当您将应用程序推送到Heroku时,它会自动调用rake assets:precompile
服务器作为部署过程中的一个步骤.但是,它在没有数据库访问或正常ENV变量的沙盒环境中执行此操作.
如果允许应用程序在资产预编译期间正常初始化,则会尝试连接到数据库时发生错误.通过将以下内容添加到application.rb文件中可以轻松解决此问题:
# Do not load entire app when precompiling assets
config.assets.initialize_on_precompile = false
Run Code Online (Sandbox Code Playgroud)
当initialize_on_precompile = false
设置,没有在初始化的config/initializers/*
被运行.我遇到的问题是我需要APP_CONFIG变量在资产预编译期间可用.
如何load_app_config.rb
在不初始化整个应用程序的情况下在资产编译期间加载?我可以使用group
传递给Rails :: Application.initialize 的参数做些什么!?
heroku ruby-on-rails-3 ruby-on-rails-3.1 asset-pipeline ruby-on-rails-3.2
我config.action_mailer.default_url_options = { :host => 'localhost:3000' }
按照建议安装了devise并添加到我的development.rb文件中.当我运行黄瓜时,我收到一个错误:
缺少主机链接!请提供:host参数或设置default_url_options [:host](ActionView :: Template :: Error)
有谁知道这与什么有关?谷歌没有太多关于此的信息
我正在关注rails3tutorial,在进行一些测试时我不明白"it"关键字的含义如下:
require 'spec_helper'
describe UsersController do
render_views
describe "GET 'new'" do
it "should be successful" do
get 'new'
response.should be_success
end
it "should have the right title" do
get 'new'
response.should have_selector("title", :content => "Sign up")
end
end
end
Run Code Online (Sandbox Code Playgroud)
代码片段来自:http://ruby.railstutorial.org/chapters/filling-in-the-layout#top上市5.26
rspec ×2
ruby ×2
activerecord ×1
block ×1
carrierwave ×1
devise ×1
factory-bot ×1
heroku ×1
lambda ×1
rvm ×1