让我们说一个User有很多Documents,还有一个Document他们正在研究的单曲.我如何在rails中表示这一点?
我想说current_user.current_document = Document.first(在文档前面有或没有current_)并且不要更改current_user.documents集合.
这就是我所拥有的:
class Document < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :documents
has_one :document
end
Run Code Online (Sandbox Code Playgroud)
问题是,当我说current_user.document = some_document,它删除以前存储在文档current_user.document的current_user.documents.这是有道理的,因为这种has_one关系Document有,但不是我想要的.我如何解决它?
我想要一个在Ruby中保持本地状态的函数.每次我调用该函数时,我想返回一个结果,该结果既依赖于调用参数又依赖于函数的存储状态.这是一个简单的例子:
def inc_mult(factor)
@state ||= 0 # initialize the state the first time.
@state += 1 # adjust the internal state.
factor * @state
end
Run Code Online (Sandbox Code Playgroud)
请注意,状态是第一次初始化,但后续调用访问存储状态.这很好,除了@state泄漏到周围的环境中,我不想要.
什么是最优雅的重写方式,以便@state不泄漏?
(注意:我的实际例子要复杂得多,初始化状态很昂贵.)
假设我在Ruby中有一个哈希,就像这样:
d = {1 => 'one', 3 => 'three', 2 =>'two'}
Run Code Online (Sandbox Code Playgroud)
我希望得到
x = [1, 2, 3]
y = ['one', 'two', 'three']
Run Code Online (Sandbox Code Playgroud)
也就是说,我想要排序的键x,以及相应的值y.我可能想要使用自定义排序顺序x.
什么是最干净,最简单的方法?
我想在Ruby中运行一个任务(比如说)10秒,如果花了更长的时间就杀掉那个任务.这是为了防止挂起外部进程.实现这个的最佳方法是什么?特别是,我将如何编写for_up_to_10_seconds下面的函数?
loop do
for_up_to_10_seconds do
# something
end
end
Run Code Online (Sandbox Code Playgroud) 您可以,如果是这样,您如何撤消css规范?假设你想要
textarea { width: 500px; }
Run Code Online (Sandbox Code Playgroud)
然后你想要一个70列的特定textarea:
<textarea class='email' cols=70></textarea>
Run Code Online (Sandbox Code Playgroud)
理想情况下,我会写一个CSS规则
textarea.email { width: revert_to_default_unspecified_value; }
Run Code Online (Sandbox Code Playgroud)
(但很明显,这个价值不存在.)
你怎么做到这一点?我对这个具体案例既感兴趣,也对如何在子代中撤消父css规范感兴趣.这一定是谈论很多,但谷歌很难.
我有以下范围来查找属于特定客户的产品.
scope :client, lambda {|client| where("client_id = ?", client) }
Run Code Online (Sandbox Code Playgroud)
并且可以被称为
Product.client(parameter)
Run Code Online (Sandbox Code Playgroud)
如果没有给出客户ID,我有什么方法可以声明我的范围返回所有产品?这是不应该使用范围的情况吗?
所以我想在Ruby中创建一个相对于其所在目录的文件路径。
我有一个项目,无论项目解压缩到哪个目录,我都希望它能够找到文件。(例如,说代码在不同的机器上运行)我一生都无法解决。
似乎要求我可以做到这一点:
require File.dirname(__FILE__) + '/comparison'
Run Code Online (Sandbox Code Playgroud)
对于与src文件夹不同目录的文件,该怎么办?
而不是列出
file = 'C:/whole path/long/very_long/file.txt'
Run Code Online (Sandbox Code Playgroud)
我想说:
file = 'file.txt'
Run Code Online (Sandbox Code Playgroud)
要么
file = File.helpful_method + 'file.txt'
Run Code Online (Sandbox Code Playgroud) 我想return_empty_set在Ruby中有一个类方法,类似于attr_reader方法.我建议的实施是
class Class
def return_empty_set *list
list.each do |x|
class_eval "def #{x}; Set.new; end"
end
end
end
Run Code Online (Sandbox Code Playgroud)
和示例用法:
class Foo
return_empty_set :one
end
Foo.new.one # returns #<Set: {}>
Run Code Online (Sandbox Code Playgroud)
但是使用一个字符串似乎是一个非常黑客.是否有更清洁或更好的方式来写这个,也许避免class_eval?或者这是最好的方式吗?
什么是用于将从浏览器接收的HTTP POST字符串转换为Ruby哈希的最佳库?我不想使用基于rails的大型库.我使用的eventmachine和evma_httpserver,并希望包括最轻库可能将解码和转换PARAMS字符串.
注意:我不需要网络服务器.我手头有编码的post字符串,只需将其转换为哈希.
我如何使用jQuery的延迟$.post?我试过了:
var myFunc = function(data, textStatus, jqXHR) {
console.log(data);
};
var post = $.post("/url/", someData);
$.when(post).done(myFunc);
Run Code Online (Sandbox Code Playgroud)
通常
$.post("/url/", someData, function(data) { myFunc(data) });
Run Code Online (Sandbox Code Playgroud)
工作正常(更改myFunc签名后).
$.when...不起作用,没有错误显示我失败.究竟是什么.done()功能传入myFunc?