我有一个ActiveRecord
模型,Foo
有一个name
字段.我希望用户能够按名称搜索,但我希望搜索忽略大小写和任何重音.因此,我还存储了一个canonical_name
要搜索的字段:
class Foo
validates_presence_of :name
before_validate :set_canonical_name
private
def set_canonical_name
self.canonical_name ||= canonicalize(self.name) if self.name
end
def canonicalize(x)
x.downcase. # something here
end
end
Run Code Online (Sandbox Code Playgroud)
我需要填写"这里的东西"来替换重音字符.还有什么比这更好的了
x.downcase.gsub(/[àáâãäå]/,'a').gsub(/æ/,'ae').gsub(/ç/, 'c').gsub(/[èéêë]/,'e')....
Run Code Online (Sandbox Code Playgroud)
而且,就此而言,由于我不在Ruby 1.9上,我不能将这些Unicode文字放在我的代码中.实际的正则表达式看起来会更加丑陋.
我正在尝试创建字符串的"规范化"副本,以帮助减少数据库中的重复名称.名称包含许多国际字符(即重音字母),我想创建一个删除了重音的副本.
我确实遇到过以下方法,但无法让它发挥作用.我似乎无法找到Unicode Hacks插件是什么.
# Utility method that retursn an ASCIIfied, downcased, and sanitized string.
# It relies on the Unicode Hacks plugin by means of String#chars. We assume
# $KCODE is 'u' in environment.rb. By now we support a wide range of latin
# accented letters, based on the Unicode Character Palette bundled inMacs.
def self.normalize(str)
n = str.chars.downcase.strip.to_s
n.gsub!(/[à áâãäåÄÄ?]/u, 'a')
n.gsub!(/æ/u, 'ae')
n.gsub!(/[ÄÄ?]/u, 'd')
n.gsub!(/[çÄ?ÄÄ?Ä?]/u, 'c')
n.gsub!(/[èéêëÄ?Ä?Ä?Ä?Ä?]/u, 'e')
n.gsub!(/Æ?/u, 'f')
n.gsub!(/[ÄÄ?ġģ]/u, 'g')
n.gsub!(/[ĥħ]/, 'h')
n.gsub!(/[ììÃîïīĩÄ]/u, 'i')
n.gsub!(/[įıijĵ]/u, 'j') …
Run Code Online (Sandbox Code Playgroud) 我有一个观察者,我注册了一个after_commit
回调.如何判断它是在创建或更新后被触发的?我可以通过询问来判断某个项目已被销毁item.destroyed?
但是#new_record?
因为该项目已保存而无法正常工作.
我打算通过添加after_create
/ after_update
并执行类似于@action = :create
内部的操作并检查@action
at 来解决它after_commit
,但似乎观察者实例是单例,我可能只是在它到达之前覆盖一个值after_commit
.所以我以一种更丑陋的方式解决了这个问题,根据after_create/update上的item.id将动作存储在地图中,并在after_commit上检查它的值.真的很难看
还有其他方法吗?
正如@tardate所说,transaction_include_action?
虽然它是一种私有方法,但它是一个很好的指示,并且在观察者中它应该被访问#send
.
class ProductScoreObserver < ActiveRecord::Observer
observe :product
def after_commit(product)
if product.send(:transaction_include_action?, :destroy)
...
Run Code Online (Sandbox Code Playgroud)
不幸的是,该:on
选项在观察者中不起作用.
只要确保你测试你的观察者的地狱(test_after_commit
如果你使用use_transactional_fixtures 寻找宝石)所以当你升级到新的Rails版本时,你会知道它是否仍然有效.
(测试3.2.9)
我现在使用ActiveSupport :: Concern代替Observers并after_commit :blah, on: :create
在那里工作.
我无法获得管理ssh密钥的系统.
我想将应用程序推送到Heroku,所以我试图推送但得到错误.
这是我的日志
$ git push heroku master
! Your key with fingerprint bf:f6:ed:14:9d:cd:52:a2:a3:16:b2:e9:b4:f2:bf:ba is not authorized to access warm-samurai-6574.
fatal: The remote end hung up unexpectedly
User@PK /e/examples (master)
$ heroku keys:add
Found existing public key: C:/Users/User/.ssh/id_rsa.pub
Uploading SSH public key C:/Users/User/.ssh/id_rsa.pub
!This key is already in use by another account. Each account must have a unique key.
User@PK /e/examples (master)
$ heroku keys
=== 1 key for denys.medynskyi@gmail.com
ssh-rsa AAAAB3NzaC...etyxYh4Q== User@PK
Run Code Online (Sandbox Code Playgroud)
每个帐户都有自己的ssh密钥.所以我可以从任何计算机上推,因为ssh键正在推送到heroku?
heroku上的每个应用程序都应该有自己的ssh密钥?
我有两个共享大部分代码的控制器(但必须是,不同的控制器).显而易见的解决方案(对我来说,至少)是创建一个类,并使两个控制器继承它.事情是......在哪里放?现在我在app_controller.php中有它,但它有点混乱.
当我在perl
在emacs模式(最近升级到GNU Emacs的23.3.1) ::
,->
以及=>
(也许还有其他符号的组合)减少到?
,?
和?
符号.这对我复制和粘贴文本的能力造成了严重破坏,并导致可读性错误.有没有办法禁用这个"功能"?
unicode ×2
utf-8 ×2
activerecord ×1
cakephp ×1
emacs ×1
heroku ×1
perl ×1
ruby ×1
ssh ×1
transactions ×1