我有一个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文字放在我的代码中.实际的正则表达式看起来会更加丑陋.
我认为这段代码可行,但正则表达式与\ r \n不匹配.我已经在十六进制编辑器中查看了我正在阅读的数据并验证了文件中确实存在十六进制D和十六进制A模式.
我也尝试过正则表达式/\xD\xA/m和/\x0D\x0A/m,但它们也不匹配.
这是我现在的代码:
lines2 = lines.gsub( /\r\n/m, "\n" )
if ( lines == lines2 )
print "still the same\n"
else
print "made the change\n"
end
Run Code Online (Sandbox Code Playgroud)
除了替代方案,我很高兴知道我做错了什么(为了方便我学习).:)
Ruby和Rails都是新手,但我现在已经预订了教育(显然没什么意义,哈哈).
我有两个模型,Event和User通过表EventUser加入
class User < ActiveRecord::Base
has_many :event_users
has_many :events, :through => :event_users
end
class EventUser < ActiveRecord::Base
belongs_to :event
belongs_to :user
#For clarity's sake, EventUser also has a boolean column "active", among others
end
class Event < ActiveRecord::Base
has_many :event_users
has_many :users, :through => :event_users
end
Run Code Online (Sandbox Code Playgroud)
这个项目是一个日历,我必须跟踪人们注册并为特定事件划出他们的名字.我认为多对多是一个很好的方法,但我做不到这样的事情:
u = User.find :first
active_events = u.events.find_by_active(true)
Run Code Online (Sandbox Code Playgroud)
因为事件实际上没有那些额外的数据,所以EventUser模型可以.虽然我能做到:
u = User.find :first
active_events = []
u.event_users.find_by_active(true).do |eu|
active_events << eu.event
end
Run Code Online (Sandbox Code Playgroud)
这似乎与"铁路方式"相反.任何人都可以启发我,今晚(今天早上)这已经困扰了我很长时间?
我有一个模块MyModule.我动态地将类加载到其中.如何获取其命名空间中定义的类的列表?
例:
def load_plugins
Dir.glob(File.dirname(__FILE__) + '/plugins/*.rb') do |f|
MyModule.class_eval File.read(f)
end
# now how can I find the new classes I've loaded into MyModule?
end
Run Code Online (Sandbox Code Playgroud)
我应该说每个都f包含类似"类Foo;结束"的内容.
您也可以这样想:在Rails中,我如何以编程方式查找ActiveRecord模块中定义的所有类?
使用Mongoid.不幸的是,Mongoid不允许选择独特/不同!得到了这些结果.如您所见,有7个结果.如果你仔细观察(在user_id),只有2个用户.
[
#<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,
#<Activity _id: 4cea6c3072357e00fa000116, created_at: 2010-11-22 13:12:16 UTC, updated_at: 2010-11-22 13:12:16 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,
#<Activity _id: 4cea6bdd72357e00fa00010d, created_at: 2010-11-22 13:10:53 UTC, updated_at: 2010-11-22 13:10:53 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,
#<Activity _id: 4cea46df72357e00fa0000a4, created_at: 2010-11-22 10:33:03 UTC, updated_at: 2010-11-22 10:33:03 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,
#<Activity _id: …Run Code Online (Sandbox Code Playgroud) 我刚刚更新到Ruby 1.9.3p0和Rails 3.1.1.现在,当我尝试启动服务器时,它抱怨我应该安装ruby-debug,即使它已经安装.
% rails server --environment=development --debug
=> Booting WEBrick
=> Rails 3.1.0 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
Exiting
Run Code Online (Sandbox Code Playgroud)
在我的Gemfile中我有
# see: http://stackoverflow.com/questions/1083451/debugging-in-ruby-1-9
gem 'ruby-debug-base19', "0.11.24"
gem 'ruby-debug19', "0.11.6"
Run Code Online (Sandbox Code Playgroud)
是否可以使用最新版本的Ruby运行调试?
我一直在阅读托马斯的编程Ruby 1.9,并发现了替代定界的单引号和双引号方法(%q / %Q).我也从其他Ruby语言参考中了解它们.
%q/I'm acting like a single-quoted string/
%Q|"I'm acting like a double-quoted string" --Anonymous|
Run Code Online (Sandbox Code Playgroud)
我没有使用Ruby很长时间,但我从未在生产代码中遇到过这种引用方法.
除了明显的避免内部使用反斜杠转义引号的能力之外,这种引用常规单引号还是双引号的方法有哪些常见用例?它们通常用于单行还是多行字符串?如果在多行字符串中使用,它们是否比HEREDOC字符串更受青睐?是否有一个特殊的Ruby成语,它们常见于何处?
我很感激gem install therubyracer上班的帮助.这是错误:
$ gem install therubyracer
Building native extensions. This could take a while...
ERROR: Error installing therubyracer:
ERROR: Failed to build gem native extension.
/Users/david/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb
checking for main() in -lobjc... yes
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
--with-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/david/.rvm/rubies/ruby-1.9.3-p194/bin/ruby …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何在ruby中递归创建目录?
在Ruby中,我该怎么做:
mkdir -p cool/beans
Run Code Online (Sandbox Code Playgroud)
这是我想出的:
Dir.mkdir('cool') unless File.directory?('cool')
cool_beans_path = File.join('cool', 'beans')
Dir.mkdir(cool_beans_path) unless File.directory?(cool_beans_path)
Run Code Online (Sandbox Code Playgroud)
但是,有没有更好的方法?
我知道我能做到:
system('mkdir', '-p', File.join('cool', 'beans'))
Run Code Online (Sandbox Code Playgroud)
但是,这不是平台独立的,是吗?比如,它适用于Mac但不适用于Windows,对吧?
ruby ×10
activerecord ×1
directory ×1
file ×1
has-many ×1
mkdir ×1
path ×1
quotes ×1
regex ×1
ruby-1.9.3 ×1
ruby-debug ×1
rubygems ×1
string ×1
time ×1
unicode ×1
unit-testing ×1
utf-8 ×1
v8 ×1