你如何在postgres中创建一个规则来强制插入X列上的小写.EG插入时:
INSERT INTO foobar (foo, bar) VALUES ('EXAMPLE', 2);
Run Code Online (Sandbox Code Playgroud)
在坚持之前,我想要EXAMPLE小写example.
我有一个数字数组按升序或降序排序,我想找到插入数字的索引,同时保留数组的顺序.如果数组是[1, 5, 7, 11, 51]和要插入的数字9,我会期待3我能这样做[1, 5, 7, 11, 51].insert(3, 9).如果数组是[49, 32, 22, 11, 10, 8, 3, 2]和要插入的数字9,我会期待,5所以我可以做[49, 32, 22, 11, 10, 8, 3, 2].insert(5, 9)
9在保留数组排序的同时,找到插入这两个数组中的任何一个的索引的最佳/最干净的方法是什么?
我编写了这段有用的代码,但它不是很漂亮:
array = [55, 33, 10, 7, 1]
num_to_insert = 9
index_to_insert = array[0..-2].each_with_index.map do |n, index|
range = [n, array[index.next]].sort
index.next if num_to_insert.between?(range[0], range[1])
end.compact.first
index_to_insert # => 3
Run Code Online (Sandbox Code Playgroud) 在linux中,使用bash,查找超过一小时但不到3天前修改过的文件的最简单方法是什么?
当然,必须有一个简单的方法来做到这一点.我一直在寻找,找不到一个简单的解决方案.
许多人谈论命名空间作为一种方式来改变导致他们的控制器带有前缀的URL(即:/ admin/movies而不是/ movies)
在官方文档解释说,如果我们想改变这种状况导致我们的前缀控制器的URL,我们只需要改变我们的资源在配置/ route.rb.
由此 :
resources :movies
Run Code Online (Sandbox Code Playgroud)
对此:
resources :movies, :path => "/admin/movies"
Run Code Online (Sandbox Code Playgroud)
经过大量的googsearches,我想知道为什么这么多人喜欢命名空间和命名空间的优点而不仅仅是修改导致路由器文件中特定控制器的URL路径:myapp/config/route.rb
为了实现命名空间,官方文档解释说您需要进行以下修改.你会发现这是很多工作:
namespace :admin do
resources :movies
end
Run Code Online (Sandbox Code Playgroud)
...并将movies_controller.rb控制器移动到app/controllers/admin目录.
但是,如果您按照这些说明操作,则会收到以下错误:
*"预期/var/www/myapp/app/controllers/admin/movies_controller.rb定义Admin :: MoviesController"*
*然后你意识到Rails期望在你的movies_controller.rb的开头写这个"外星人"的写作:"Admin ::"*
所以你改变了movies_controller.rb的第一行:
class admin::MoviesController < ApplicationController
Run Code Online (Sandbox Code Playgroud)
代替 :
class MoviesController < ApplicationController
Run Code Online (Sandbox Code Playgroud)
但是你再次得到另一个错误: "未定义的局部变量或方法`admin'for main:Object"
然后你意识到你忘了Ruby类必须用起始大写声明.
所以你在Ruby类中添加一个起始大写:
class Admin::MoviesController < ApplicationController
Run Code Online (Sandbox Code Playgroud)
但是你仍然会收到一个错误:
*缺少模板admin/movies/index,application/index with {:locale => [:"fr-CH"],:formats => [:html],:handlers => [:erb,:builder,:rxls, :咖啡,:haml]}.搜索:*"/ var/www/myapp/app/views"*
我勒个去...?哦,文档忘了说控制器的相应视图也必须移动到app/view /中的管理目录吗?
所以你创建一个目录并将你的视图移动到app/view/admin /你仍然会得到同样的错误.
然后你意识到你忘记在app/view/admin中包含电影文件夹了所以你做到了.
你仍然收到一条错误信息:*###:0xa817c0c的未定义方法`movies_path'*
这次你知道路由,控制器和视图工作,但你仍然需要更改所有视图的路径... …
该网站正常工作,直到我添加config.serve_static_assets = false我的production.rb.然后我得到这个:
demosite.com(master)$ RAILS_ENV=production rails s
WARNING: Use strings for Figaro configuration. false was converted to "false".
=> Booting Puma
=> Rails 4.1.4 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
Exiting
/Users/user1/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:125:in `assert_index': No such middleware to insert before: ActionDispatch::Static (RuntimeError)
from /Users/user1/.rvm/gems/ruby-2.2.1/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:88:in `insert'
from /Users/user1/.rvm/gems/ruby-2.2.1/gems/railties-4.1.4/lib/rails/configuration.rb:68:in `block in merge_into'
from …Run Code Online (Sandbox Code Playgroud) 如何使用$in复合索引有效地查找集合?
下面的示例中,索引位于字段 a 和 b 上。例如:db.foo.createIndex({a: 1, b: 1})
SQL 中的示例:
SELECT *
FROM foo
WHERE (a,b)
IN (
("aVal1", "bVal1"),
("aVal2", "bVal2")
);
Run Code Online (Sandbox Code Playgroud)
我知道你可以这样做:
db.foo.find( {
$or: [
{ a: "aVal1", b: "bVal1" },
{ a: "aVal2", b: "bVal2" },
]
} )
Run Code Online (Sandbox Code Playgroud)
是否有更高效的方法来使用$in运算符来执行此操作?
I have been reading through the documentation and I can't find what I'm doing wrong here.
I'm executing this query:
SELECT *
FROM "parts"
INNER JOIN "categories" ON "categories"."id" = "parts"."category_id"
WHERE "categories"."name" = "cars"
Run Code Online (Sandbox Code Playgroud)
And I'm getting this error:
ERROR: column "cars" does not exist
LINE 3: WHERE (categories.name = "cars")
^
********** Error **********
ERROR: column "cars" does not exist
SQL state: 42703
Character: 122
Run Code Online (Sandbox Code Playgroud)
Category Table:
CREATE TABLE categories
(
id serial NOT NULL,
name character varying(255), …Run Code Online (Sandbox Code Playgroud) 我希望我的产品网址看起来像:
/product-name-here/p
代替:
/product/product-name-here
我怎样才能做到这一点?
我有一个新的rails安装.我完全按照本页上的说明操作:https://github.com/rweng/jquery-datatables-rails
当我尝试查看数据表时,我收到以下错误:
GET http://localhost:3000/images/sort_both.png 404 (Not Found)
GET http://localhost:3000/images/sort_asc.png 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)
在config/environments/development.rb我尝试更改以下设置:
config.assets.debug = false #and also tried true
config.serve_static_assets = true #and also tried false
config.assets.enabled = true
Run Code Online (Sandbox Code Playgroud)
我也试过跑步 rake assets:precompile
不知道我在这里缺少什么.任何帮助将不胜感激.
我对该模型中的范围有疑问,并且不知道如何最好地解决它。我想distinct_mechanics_sql在范围内调用 from 以使事情更清晰,但是当我运行它时,我收到“未定义的局部变量或方法”错误。这是模型:
class Mechanics < ActiveRecord::Base
scope :with_moz, -> { joins("JOIN (#{distinct_mechanics_sql}) mh ON mh.mechanic_id = mechanics.id") }
def distinct_mechanics_sql
"""
SELECT DISTINCT ON (mechanic_id) *
FROM checkups
ORDER BY mechanic_id, updated_at DESC
"""
end
end
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
ruby ×3
postgresql ×2
activerecord ×1
bash ×1
jquery ×1
linux ×1
mongodb ×1
namespaces ×1
spree ×1
sql ×1
url-routing ×1