Ruby 和 Rails 常见问题

0 条评论 , 9 次修正,1532 次阅读,最后更新于 2012年03月21日

怎么提取一段 text 里所有的 url?

https://github.com/twitter/twitter-text-rb 里面有个 autolink 方法,考虑的比较全面。 可以用 https://github.com/twitter/twitter-text-rb/blob/master/lib/regex.rb 里面的 Twitter::Regex::REGEXEN[:valid_url] 来匹配。

为什么...开发模式是/assets/application.js,生产模式就成了/javascripts/application.js

是不是没有 Uncomment mongoid 里的那一行?

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# require "sprockets/railtie" # Uncomment this line for Rails 3.1+

Mongoid

 Mongoid里的embeds_many embeds_in 和 has_many belongs_to 区别在哪里?

http://mongoid.org/docs/relations.html
embeds_many是Embedded relations
has_many是Referenced relations

*Embedded relations* are associations between one or many objects **where the child object is embedded within the parent in the same document in the database**.
They can be extremely efficient when kept to a managable size in MongoDB since the total number of queries required to retrieve an entire object graph can be reduced to one.

*Referenced relations* are associations between documents that **reside in separate collections in MongoDB**.
The link between the documents is handled similar to a relational database where a "foreign key" needs to be stored on one or both sides of the relation, but note that accessing a referenced relation required a separate query to the database since joins cannot be performed.