相关疑难解决方法(0)

Rails模型中不区分大小写的搜索

我的产品型号包含一些商品

 Product.first
 => #<Product id: 10, name: "Blue jeans" >
Run Code Online (Sandbox Code Playgroud)

我现在从另一个数据集导入一些产品参数,但名称的拼写有不一致之处.例如,在其他数据集中,Blue jeans可以拼写Blue Jeans.

我想Product.find_or_create_by_name("Blue Jeans"),但这将创造一个新产品,几乎与第一个相同.如果我想找到并比较小写的名字,我有什么选择.

性能问题在这里并不重要:只有100-200个产品,我想将其作为导入数据的迁移来运行.

有任何想法吗?

activerecord ruby-on-rails case-insensitive

206
推荐指数
12
解决办法
11万
查看次数

Rails Postgres功能索引

我应该如何将包含函数的multicolum索引输入schema.rb?

例如,这不起作用:

add_index "temporary_events", ["templateinfoid", "campaign", "date(gw_out_time)", "messagetype"], :name => "temporary_events_campaign_tinfoid_date_messagetype"
Run Code Online (Sandbox Code Playgroud)

rake db:test:load

耙子流产了!

PGError:错误:列"date(gw_out_time)"不存在

:CREATE INDEX"temporary_events_campaign_tinfoid_date_messagetype"ON"temporary_events"("templateinfoid","campaign","date(gw_out_time","messagetype")

sql postgresql indexing ruby-on-rails

7
推荐指数
2
解决办法
4644
查看次数

schema.rb中的LOWER索引

在我的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中看到这一点.

ruby sql postgresql ruby-on-rails

5
推荐指数
1
解决办法
275
查看次数